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
Python/Clojure style multimethod that dispatches on type of first argument.
const singledispatch = fallback => {
let _key = Symbol('singledispatch method')
// Assign fallback to Object prototype.
// This makes it the method of last resort.
Object.prototype[_key] = fallback
const dispatch = (object, ...rest) => {
let method = object[_key]
return method(object, ...rest)
}
dispatch.define = (constructor, method) => {
constructor.prototype[_key] = method
}
return dispatch
}
class Coords {
constructor(x, y) {
this.x = x
this.y = y
}
add(x, y) {
this.x = this.x + x,
this.y = this.y + y
}
}
const vanillaAdd = (
coords,
x,
y
) => {
coords.x = coords.x + x,
coords.y = coords.y + y
}
const add = singledispatch()
add.define(
Coords,
(coords, x, y) => {
coords.x = coords.x + x,
coords.y = coords.y + y
}
)
let origin = new Coords(0, 0)
Ready to run.
Test | Ops/sec | |
---|---|---|
add singledispatch |
| ready |
add method |
| ready |
vanilla add |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.