Skip to content

Instantly share code, notes, and snippets.

@wilf312
Created October 15, 2020 06:04
Show Gist options
  • Save wilf312/4885d8ff9f62049993bf505ce388eecf to your computer and use it in GitHub Desktop.
Save wilf312/4885d8ff9f62049993bf505ce388eecf to your computer and use it in GitHub Desktop.
JS set vs obj performance
var N = 100000000
var theSet = new Set();
console.time()
/***********************************/
for(let i = 0 ; i < N; i++ ) {
let v = i%20;
if (!theSet.has(v))
theSet.add(v);
}
/***********************************/
console.timeEnd()
console.log('section 1 : ');
obj = {};
console.time()
/***********************************/
for(let i = 0 ; i < N; i++) {
let v = i%20;
if (!obj[v])
obj[v] = 1;
}
/***********************************/
console.timeEnd()
console.log('section 2 : ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment