-
-
Notifications
You must be signed in to change notification settings - Fork 869
Improve array parsing performance #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,7 +116,10 @@ var merge = function merge(target, source, options) { | |
| } | ||
|
|
||
| if (isArray(target) && isArray(source)) { | ||
| source.forEach(function (item, i) { | ||
| var sourceOwnProperties = Object.getOwnPropertyNames(source); | ||
| for (var j = 0; j < sourceOwnProperties.length - 1; j++) { | ||
| var i = sourceOwnProperties[j]; | ||
| var item = source[i]; | ||
| if (has.call(target, i)) { | ||
| var targetItem = target[i]; | ||
| if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') { | ||
|
|
@@ -127,7 +130,7 @@ var merge = function merge(target, source, options) { | |
| } else { | ||
| target[i] = item; | ||
| } | ||
| }); | ||
| } | ||
| return target; | ||
| } | ||
|
|
||
|
|
@@ -287,9 +290,17 @@ var combine = function combine(a, b, arrayLimit, plainObjects) { | |
| return a; | ||
| } | ||
|
|
||
| var result = [].concat(a, b); | ||
| if (result.length > arrayLimit) { | ||
| return markOverflow(arrayToObject(result, { plainObjects: plainObjects }), result.length - 1); | ||
| var length; | ||
| var result; | ||
| if (Array.isArray(a)) { | ||
| length = Array.isArray(b) ? a.push.apply(a, b) : a.push(b); | ||
| result = a; | ||
| } else { | ||
| result = [].concat(a, b); | ||
| length = result.length; | ||
| } | ||
| if (length > arrayLimit) { | ||
| return markOverflow(arrayToObject(result, { plainObjects: plainObjects }), length - 1); | ||
|
Comment on lines
+293
to
+303
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changing from "only relying on .concat" to "relying on push and apply and concat" isn't an improvement, unfortunately. also, we can't rely on
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that the code isn't the most beautiful with 3 different cases of combining arrays, but it is much faster. I can't think of a faster way to combine 2 arrays without relying on Node.js |
||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -143,9 +143,9 @@ test('combine()', function (t) { | |||||
| var b = [2]; | ||||||
| var combined = utils.combine(a, b); | ||||||
|
|
||||||
| st.deepEqual(a, [1], 'a is not mutated'); | ||||||
| st.deepEqual(a, [1, 2], 'a is mutated'); | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if tests are changed, it's a breaking change. tests should not be changed.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Lines 235 to 236 in 6bdfaf5
I believe that in #185 (comment) you were willing to accept mutation if you see real performance improvements and there certainly is one for 100 elements (over 2x for whole I'll admit that there may be some way to break this with custom |
||||||
| st.deepEqual(b, [2], 'b is not mutated'); | ||||||
| st.notEqual(a, combined, 'a !== combined'); | ||||||
| st.deepEqual(a, combined, 'a === combined'); | ||||||
| st.notEqual(b, combined, 'b !== combined'); | ||||||
| st.deepEqual(combined, [1, 2], 'combined is a + b'); | ||||||
|
|
||||||
|
|
@@ -167,9 +167,9 @@ test('combine()', function (t) { | |||||
| st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array'); | ||||||
|
|
||||||
| var combinedABn = utils.combine(a, bN); | ||||||
| st.deepEqual(a, [aN], 'a is not mutated'); | ||||||
| st.deepEqual(a, [aN, bN], 'a is mutated'); | ||||||
| st.notEqual(aN, combinedABn, 'a + bN !== aN'); | ||||||
| st.notEqual(a, combinedABn, 'a + bN !== a'); | ||||||
| st.deepEqual(a, combinedABn, 'a + bN === a'); | ||||||
| st.notEqual(bN, combinedABn, 'a + bN !== bN'); | ||||||
| st.notEqual(b, combinedABn, 'a + bN !== b'); | ||||||
| st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array'); | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can't rely on Object.getOwnPropertyNames existing. additionally,
sourceis an array here, so why wouldn't we want to just iterate from 0 tosource.length?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not notice that
qssupports Node.js versions older than0.10, sorry.As I wrote in the description, when parsing indexed arrays
merge()is called for each element withtargetbeing the "accumulated" array andsourcea sparse array with only one element..forEachinternally isfor (let k = 0; k < arr.length; k++) { if (arr.hasOwn(k) { cb(arr[k], k, arr); } }(ArrayForEachLoopContinuationin V8). With large sparse arrays this takes some time and combined with repeatedly callingmerge()is O(n^2).This is the less practical part of the PR, because for values of
parameterLimitandarrayLimitup to 1000 (arrayLimitis smaller by default), the current code is still faster.