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
Sort by number (without mutation)
const items = Array.from({ length: 1e3 }).map((_, index) => {
const publishedAt = Math.floor(Date.now() * Math.random())
return {
publishedAt,
name: 'Article ' + index }
})
const sortByNumber = (items, key) => {
const l = items.length
const b = 32 - Math.clz32(l)
const m = (1 << b) - 1
const tmp = new Float64Array(l)
const res = Array(l)
let i = l
let j = l
while (i--) {
const item = items[i]
const v = item[key]
if (typeof v === 'number') {
tmp[i] = (v << b) | i
} else {
res[--j] = item
}
}
const sorted = tmp.subarray(0, j).sort()
while (j--) {
res[j] = items[sorted[j] & m]
}
return res
}
Ready to run.
Test | Ops/sec | |
---|---|---|
native sort |
| ready |
native toSorted |
| ready |
sortByNumber |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.