Skip to content

Commit

Permalink
faster getProperty; 1.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Sep 17, 2020
1 parent 3235a17 commit 0f3f7ce
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ else a Buffer for Buffer data. The callback is invoked when the 'end' event is
Changelog
---------

- 1.6.1 - faster `getProperty`
- 1.6.0 - new function `entries`, `sort3i`; new undocumented functions `str_locate`, `randomize`, `interleave2`, `groupBy`, `sortBy`, `range`, `clone`;
fix get/setIterator property name; speed up iterators, change makeIterator step func args; faster str_random
- 1.5.1 - fix getProperty, do not prevent multiple callbacks from readBody
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qibl",
"version": "1.6.0-dev",
"version": "1.6.1",
"description": "many small useful functions",
"keywords": [ "repeat", "fill", "assign", "merge", "Buffer", "polyfill", "toStruct", "subsample" ],
"main": "qibl.js",
Expand Down
21 changes: 13 additions & 8 deletions qibl.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,24 @@ function merge( target /* ,VARARGS */ ) {
* node-v10 and up tokenize: 1.6m/s v8, 3.7m/s v13, 2.4m/s v0.10
* strtok: 2m/s v8, 4.4m/s v13, 2.47m/s v0.10
* note: defaultValue support makes it 15% slower
* note: since node-v12 this function is much slower, dropped from 13m/s to 7m/s
*/
function getProperty( target, dottedName, defaultValue ) {
if (typeof target === 'string' && isMethodContext(this)) return getProperty(this, target, dottedName);
if (!target) return defaultValue;

var first, path;
if (dottedName.indexOf('.') < 0) { first = dottedName; path = [] } else { path = dottedName.split('.'); first = path[0] }
var isDotted = dottedName.length > 40;
if (!isDotted) for (var i = 0; i < dottedName.length; i++) { if (dottedName.charCodeAt(i) === 0x2E) { isDotted = true; break; } }
if (!isDotted) return (target && target[dottedName] !== undefined ? target[dottedName] : defaultValue);

target = target[first];
for (var i = 1; i < path.length; i++) {
if (target == null) return defaultValue;
target = target[path[i]];
}
var path = dottedName.split('.');
target = target == null ? undefined : target[path[0]];
target = target == null ? undefined : target[path[1]];
if (path.length > 2) {
target = target == null ? undefined : target[path[2]];
if (path.length > 3) {
target = target == null ? undefined : target[path[3]];
}}
for (var i = 4; i < path.length; i++) target = target == null ? undefined : target[path[i]];
return target !== undefined ? target : defaultValue;
}

Expand Down
10 changes: 10 additions & 0 deletions test-qibl.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ module.exports = {
[{a:1, b:{c:{d:2}}}, 'b.c', {d:2}],
[{a:1, b:{c:{d:2}}}, 'b.c.d', 2],
[{a:1, b:{c:{d:2}}}, 'b.c.d.e', undefined],

[{a:null}, 'a', null],
[{a: {b: null}}, 'a.b', null],
[{a: {b: {c: null}}}, 'a.b.c', null],
[{a: {b: {c: {d: null}}}}, 'a.b.c.d', null],
[{a: {b: {c: {d: {e: null}}}}}, 'a.b.c.d.e', null],
[{a: {b: {c: {d: {e: {f: null}}}}}}, 'a.b.c.d.e.f', null],
[{a: {b: {c: {d: {e: {f: null}}}}}}, 'a.b.c', {d: {e: {f: null}}}],
];

for (var i=0; i<tests.length; i++) {
Expand All @@ -169,6 +177,8 @@ module.exports = {

[false, 'a'],
[null, 'a'],
[null, 'a.b'],
[null, 'a.b.c'],
[0, 'a'],
[undefined, 'a'],
];
Expand Down

0 comments on commit 0f3f7ce

Please sign in to comment.