Comments (7)
prop in obj
is slow
yes, true. But not that slow, and unfortunately it's necessary to be able to get non-enumerable prototype methods.
extra params a, b, c could be deleted
we can't remove that feature because it's used extensively in a number of projects. It allows you to dynamically create keys without having to slice/concat args then .join
keys, thus it actually results in a speed increase "round trip".
var obj = {a: {b: {c: 'd'}}};
get(obj, ['a', 'b', 'c']);
// is the equivalent of
get(obj, 'a.b.c');
So, for example, let's say you want to create a custom get
method where the key is always prefixed with a specific property path:
function Views() {
this.views = {};
}
Views.prototype.set = function(key) {
set(this, key, val);
return this;
};
Views.prototype.get = function(key) {
return get(this, key);
};
Views.prototype.getView = function(key) {
// ignore the fact that we can just use `get(this.views,..)` for a moment
// this allows us to not have to join `views` to `?`, which would need to
// be split again later
return this.get(['views', key]);
};
Of course, in this example you could easily just use get directly and do get(this.views, key)
, but it's not as easy when you're inheriting or using an instance of something with a get
method. We do this a lot in libs like base, since it will always be faster than checking arguments and joining the keys, then splitting them again in get-value
Plus, in the benchmarks that code does not have much of an impact on speed, since it's explicit.
hope that makes sense.
from get-value.
I see. That's the reason why this lib is slower than dot-prop
(around 40-50% in the benchmark test).
I suggest highlighting this feature in README.
I understand the feature but never considered that use case.
Then we should also add this feature to README. I would stress that only 3 params are supported (which would fit most use case).
from get-value.
(around 40-50% in the benchmark test).
Interesting, I get 2.8m ops/sec for this lib versus 3m ops/sec for dot prop. what version of node are you running?
I would stress that only 3 params are supported (which would fit most use case).
good idea. closing this, but I'll open a new one for docs
from get-value.
I'm using Node 5.1.
I can post more results later.
On Fri, Jan 22, 2016 at 12:50 PM, Jon Schlinkert [email protected]
wrote:
—
Reply to this email directly or view it on GitHub
#5 (comment).
from get-value.
$ node -v; npm -v
v5.1.0
3.5.0
$ npm ls --depth 0
[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
# master
$ node benchmark
#1: deep
current x 2,664,756 ops/sec ±0.56% (98 runs sampled)
dot-prop x 3,796,504 ops/sec ±0.47% (97 runs sampled)
getobject x 261,985 ops/sec ±0.30% (99 runs sampled)
fastest is dot-prop
#2: shallow-long
current x 4,119,104 ops/sec ±0.39% (97 runs sampled)
dot-prop x 6,463,488 ops/sec ±0.28% (95 runs sampled)
getobject x 840,169 ops/sec ±0.52% (95 runs sampled)
fastest is dot-prop
#3: shallow
current x 4,080,770 ops/sec ±0.20% (98 runs sampled)
dot-prop x 6,287,998 ops/sec ±0.54% (95 runs sampled)
getobject x 727,382 ops/sec ±1.18% (90 runs sampled)
fastest is dot-prop
# commented `prop in obj`
$ node benchmark
#1: deep
current x 3,452,616 ops/sec ±0.47% (96 runs sampled)
dot-prop x 3,681,697 ops/sec ±0.52% (96 runs sampled)
getobject x 270,249 ops/sec ±0.51% (99 runs sampled)
fastest is dot-prop
#2: shallow-long
current x 5,696,825 ops/sec ±0.52% (96 runs sampled)
dot-prop x 6,433,232 ops/sec ±0.42% (93 runs sampled)
getobject x 869,137 ops/sec ±0.56% (96 runs sampled)
fastest is dot-prop
#3: shallow
current x 5,616,723 ops/sec ±0.37% (99 runs sampled)
dot-prop x 6,444,482 ops/sec ±0.67% (96 runs sampled)
getobject x 924,384 ops/sec ±1.11% (93 runs sampled)
fastest is dot-prop
from get-value.
$ node -v; npm -v
v4.1.1
2.14.4
$ npm ls --depth 0
[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
# master
$ node benchmark
#1: deep
current x 2,569,185 ops/sec ±0.38% (97 runs sampled)
dot-prop x 3,583,916 ops/sec ±0.33% (99 runs sampled)
getobject x 273,026 ops/sec ±0.16% (99 runs sampled)
fastest is dot-prop
#2: shallow-long
current x 3,950,008 ops/sec ±0.60% (92 runs sampled)
dot-prop x 6,304,140 ops/sec ±0.32% (96 runs sampled)
getobject x 863,239 ops/sec ±0.36% (98 runs sampled)
fastest is dot-prop
#3: shallow
current x 3,842,925 ops/sec ±0.14% (100 runs sampled)
dot-prop x 6,284,504 ops/sec ±0.42% (100 runs sampled)
getobject x 918,652 ops/sec ±0.30% (99 runs sampled)
fastest is dot-prop
# commented `prop in obj`
$ node benchmark
#1: deep
current x 3,213,149 ops/sec ±0.45% (96 runs sampled)
dot-prop x 3,587,530 ops/sec ±0.55% (96 runs sampled)
getobject x 273,838 ops/sec ±0.36% (100 runs sampled)
fastest is dot-prop
#2: shallow-long
current x 5,369,434 ops/sec ±0.38% (96 runs sampled)
dot-prop x 5,904,589 ops/sec ±0.31% (97 runs sampled)
getobject x 873,513 ops/sec ±0.66% (94 runs sampled)
fastest is dot-prop
#3: shallow
current x 5,412,105 ops/sec ±0.26% (91 runs sampled)
dot-prop x 6,230,873 ops/sec ±0.76% (96 runs sampled)
getobject x 890,037 ops/sec ±0.38% (96 runs sampled)
fastest is dot-prop
from get-value.
Thanks for posting it. Clearly I like getting the best performance possible, or I wouldn't waste my time with adding benchmarks. But IMHO it's more important to actually pass the unit tests, and last time I checked dot-prop failed several of them
from get-value.
Related Issues (19)
- Benchmark against object-path HOT 2
- Unexpected Identifier and WebpackJsonp not defined on building Android in Ionic HOT 1
- Please pre-compiled before publish HOT 2
- Issue with Default Value of 0 HOT 2
- default value is useless without isValid call, which defaults to true for everything HOT 2
- benckmark compare with object.get HOT 1
- document escaping dot HOT 1
- how to get a path of a key? HOT 2
- Question - any significant differences between this library and lodash's _.get() ? HOT 1
- benchmark optional chaining HOT 1
- update dep
- missing index.d.ts
- collection not accessible HOT 2
- Support array notation in property path HOT 15
- docs: mention support for passing an array for `key`
- Using array notation breaks when values has periods HOT 7
- Open to PR with `get(...default='default')`? HOT 2
- npmjs package does not include readme HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from get-value.