jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
I want to understand the difference between
new Set(arrayOfItems.map(item => item.id))
and
const theSet = new Set(); arrayOfItems.forEach(item => theSet.add(item.id));
Constraints: All items will have an ID - no benefit from filtering first. Ids can be duplicates (hence the need for a set)
const arrayOfItems = new Array(100_000);
function randomizeArrayIds(array) {
for (var i = array.length - 1; i > 0; i--) {
array[i] = { id: Math.floor(Math.random() * 1000) };
}
}
randomizeArrayIds(arrayOfItems);
Ready to run.
Test | Ops/sec | |
---|---|---|
new Set(arrayOfItems.map(item => item.id)); |
| ready |
arrayOfItems.forEach(item => theSet.add(item.id)); |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.