Skip to content

Instantly share code, notes, and snippets.

@davemackintosh
Forked from AdriVanHoudt/bench.js
Last active January 21, 2016 12:42
Show Gist options
  • Save davemackintosh/4feed6df6ea703e3c594 to your computer and use it in GitHub Desktop.
Save davemackintosh/4feed6df6ea703e3c594 to your computer and use it in GitHub Desktop.
Mini benchmark Hoek.unique vs Set
"use strict"
// used in benchmarks.
const SAMPLE = 5000000
const Hoek = require('hoek')
const array = []
for (let i = 0; i < SAMPLE; ++i) {
array.push(i * Math.floor(Math.random() * (10 - 1 + 1)) + 1)
}
const sorted = array.sort()
console.time('hoek unsorted')
const result = []
for (let i = 0; i < array.length; ++i) {
result.push(array[i])
}
const resultHoek = Hoek.unique(result)
console.timeEnd('hoek unsorted')
console.time('set unsorted')
const result2 = new Set()
for (let i = 0; i < array.length; ++i) {
result2.add(array[i])
}
const result2Set = Array.from(result2)
console.timeEnd('set unsorted')
console.time('hoek sorted')
const result3 = []
for (let i = 0; i < sorted.length; ++i) {
result3.push(sorted[i])
}
const resultHoek2 = Hoek.unique(result3)
console.timeEnd('hoek sorted')
console.time('set sorted')
const result4 = new Set()
for (let i = 0; i < sorted.length; ++i) {
result4.add(sorted[i])
}
const result2Set2 = Array.from(result4)
console.timeEnd('set sorted')
@AdriVanHoudt
Copy link

reference

davidmackintosh in /tmp $ node -v
v5.1.1
davidmackintosh in /tmp $ node bench.js 
hoek unsorted: 5516.160ms
set unsorted: 2684.325ms
hoek sorted: 5244.573ms
set sorted: 3053.828ms

davidmackintosh in /tmp $ nvm use 4.2.2
Now using node v4.2.2
davidmackintosh in /tmp $ node bench.js 
hoek unsorted: 5786ms
set unsorted: 3324ms
hoek sorted: 5191ms
set sorted: 2900ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment