Created
November 2, 2019 22:08
-
-
Save andrey-legayev/142ad2e2a00980e6da3776d40636fbfe to your computer and use it in GitHub Desktop.
JS Benchmark: test performance of Set()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test(size, iterations) { | |
let data_array = []; | |
let data_obj = {}; | |
for (let i = 0; i < size; i++) { | |
let rnd = Math.floor(Math.random() * size * 10); | |
data_array.push(rnd); | |
data_obj[rnd] = true; | |
} | |
let data_set = new Set(data_array); | |
let t = performance.now(); | |
for (let j = 0; j < iterations; j++) { | |
let x = data_array.indexOf(j) !== -1; | |
} | |
let t1 = performance.now() - t; | |
t = performance.now(); | |
for (let j = 0; j < iterations; j++) { | |
let x = data_obj[j]; | |
} | |
let t2 = performance.now() - t; | |
t = performance.now(); | |
for (let j = 0; j < iterations; j++) { | |
let x = data_set.has(j); | |
} | |
let t3 = performance.now() - t; | |
console.log([size, iterations, t1/1000, t2/1000, t3/1000].join(", ")); | |
} | |
console.log("# Array Size, Iterations, in array, in object, in set"); | |
for (let i = 1; i <= 10000; i *= 10) { | |
test(i, 100000) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm surprised: set.has() is a bit faster than object[key]
Tested in Node.js 10 and Chrome 78