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
feat: upgrade remove duplicates performance #8
Conversation
Thanks! This version improves the performance greatly when there are a lot of elements concentrated in one grid. The original use of Thank you for your library, @timohausmann. Can you review this PR? |
Thanks for the PR and reminder. Can you provide an example where this improves performance? In the many to many example I get ~10 fps less with 6400 objects. My first thought on refactoring duplicate removal would be a Set like The check |
Yes, if(this.level === 0) is important, and using a WeakMap can reduce the complexity of removing duplicates from O(n^2) to O(n). The reason you get fewer FPS is due to the draw() function(because retrieve is more fast LoL). We should only compare the performance of the retrieve function. here is an example: // simple script.js
// Log cost time
console.time("retrieve");
const candidates = tree.retrieve(myCursor);
console.timeEnd("retrieve");
// Add many objects
document.getElementById("btnAdd10").addEventListener("click", function () {
for (var i = 0; i < 1000000; i++) {
handleAdd();
}
});
// Here, disable render to avoid side effects.
function draw() {
return;
} Then, when comparing the two |
I have optimized the deduplication logic again. The current logic is clearer, and the test speed has slightly improved compared to the previous version I submitted (the complexity remains the same, perhaps because some data access has been reduced). |
Okay right, we should measure retrieve function only. Here is what I got:
That's a great improvement! Can your WeakMap beat Set though? It's basically a one-liner: if(this.level === 0) {
return [... new Set(returnObjects)];
} Performance script: const objectAmount = 1000000;
const times = 100;
let all = 0;
for(var i=0;i<objectAmount;i++) {
handleAdd();
};
for(let i=0; i<times; i++) {
// console.time("retrieve");
const start = window.performance.now();
tree.retrieve(myCursor);
// console.timeEnd("retrieve");
const end = window.performance.now();
const time = end - start;
// console.log('retrieve', time);
all += time;
}
console.log('ran', times, 'times, average:', (all / times), 'ms') |
Okay, I'm using |
Good stuff, thanks. |
No description provided.