-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Comparing arrays with assert
seems to take polynomial time.
#12842
Comments
assert
seems to take O(exp(n)) time.assert
seems to take polynomial time.
tl;dr: Yes In OSS as in OSS, you are welcome to submit an improvement. If you are interested a good place to start is https://github.com/nodejs/node/blob/master/lib/assert.js#L392 |
A simpler option is to add a comment in the docs https://github.com/nodejs/node/blob/master/doc/api/assert.md#assertdeepequalactual-expected-message |
It should be O(n²) right now, I think – it’s iterating over the array in a linear fashion, but currently does an This is fixable (to O(n log n)), though: #12849 |
Use a Map instead of an array for checking previously found cyclic references. This reduces complexity for an array-of-objects case from O(n²) to O(n·log n). Fixes: nodejs#12842 PR-URL: nodejs#12849 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Rich Trott <[email protected]>
Use a Map instead of an array for checking previously found cyclic references. This reduces complexity for an array-of-objects case from O(n²) to O(n·log n). Fixes: nodejs#12842 PR-URL: nodejs#12849 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Rich Trott <[email protected]>
Comparing two arrays with
assert.deepEqual
or.deepStrictEqual
seems to take a polynomial amount of time relative to the array size.Quick code:
Returns a list of (n),(time) pairs.
On my machine, the output of this is
Graphed, this looks like
Or to illustrate what I think I'm seeing, we can plot on a log-log scale graph:
Just to be explicit, I would expect this operation to take O(n) time.
I'm quite possibly doing something very silly, so apologies in advance if I'm unknowingly spluttering over my own mistakes.
The text was updated successfully, but these errors were encountered: