-
-
Notifications
You must be signed in to change notification settings - Fork 184
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
Optimise dict equality check on JavaScript #738
Conversation
I'll look into adding ES6-style iterators and see what the performance is like. |
b5ffd1f
to
1821ff0
Compare
The fastest ES6 iterator I came up with iterates at about half the speed of the Here's the code: /**
* @template K,V
* @param {undefined | Node<K,V>} root
* @returns {IterableIterator<[K, V]>}
*/
function iterator(root) {
// Return an empty iterator if there's no root node
if (root === undefined) {
return [][Symbol.iterator]();
}
// Stack of nodes in the trie that tracks the iterator's current position
const nodes = [[root?.array, 0]];
// Quick access to the current node being iterated
let currentNodeItems = nodes[0][0];
let currentNodeItemIndex = nodes[0][1];
return {
next() {
while (true) {
// If the current node is fully iterated then pop it off the stack
if (currentNodeItemIndex >= currentNodeItems.length) {
nodes.pop();
// Finish when the final node is popped off
if (nodes.length === 0) {
return { done: true };
}
currentNodeItems = nodes[nodes.length - 1][0];
currentNodeItemIndex = nodes[nodes.length - 1][1];
} else {
const item = currentNodeItems[currentNodeItemIndex];
// Move the current node to its next item
currentNodeItemIndex++;
if (item === undefined) {
continue;
} else if (item.type === ENTRY) {
return { value: [item.k, item.v], done: false };
} else {
nodes[nodes.length - 1][1] = currentNodeItemIndex;
// Iterate into this child node
nodes.push([item.array, 0]);
currentNodeItems = item.array;
currentNodeItemIndex = 0;
}
}
}
},
[Symbol.iterator]() {
return this;
},
};
} It may be hard/impossible to match the speed of I don't know of any 3rd-party uses of |
ab07691
to
e10f6aa
Compare
I agree, that's too much of a performance hit. Returning |
e10f6aa
to
3a46c8f
Compare
ac586b1
to
bee3e1f
Compare
bee3e1f
to
af8f71d
Compare
Ah right. It's because The code looks good to me, it is ready to merge? Or does that catch cause a problem? |
Ready to merge 👍 |
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.
Thank you!!
The dict equality check now returns false as soon as any item doesn't match, rather than continuing to unnecessarily check remaining items.