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
const items = Array.from({ length: 100e3 }).map(() => {
const age = Date.now()
return {
age,
name: 'Youzi ' + age,
}
})
const sortCopyByNumber = (items, key) => {
let i = items.length
let j = 0
let k = i
const tmp = new Float64Array(i)
const map = {}
const res = Array(i)
while (i--) {
const item = items[i]
const v = item[key]
if (typeof v !== 'number') {
res[--k] = item
} else if (v in map) {
map[v].push(item)
} else {
tmp[j] = v
map[v] = [item]
j++
}
}
const sorted = tmp.subarray(0, j).sort()
for (const v of sorted) {
const arr = map[v]
let l = arr.length
while (l--) {
res[++i] = arr[l]
}
}
return res
}
const sortCopyByNumber2 = (items, key) => {
let i = items.length
let j = 0
let k = i
const tmp = new Uint16Array(i)
const map = {}
const res = Array(i)
while (i--) {
const item = items[i]
const v = item[key]
if (typeof v !== 'number') {
res[--k] = item
} else if (v in map) {
map[v].push(item)
} else {
tmp[j] = v
map[v] = [item]
j++
}
}
const sorted = tmp.subarray(0, j).sort()
for (const v of sorted) {
const arr = map[v]
let l = arr.length
while (l--) {
res[++i] = arr[l]
}
}
return res
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Array.from(items).sort(fn) |
| ready |
sortCopyByNumber |
| ready |
sortCopyByNumber2 |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.