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
}
clone() {
return new Coords(
this.x,
this.y
)
}
add(x, y) {
return new Coords(
this.x + x,
this.y + y
)
}
}
const clone = singledispatch()
clone.define(
Coords,
coords => new Coords(coords.x, coords.y)
)
const add = singledispatch()
add.define(
Coords,
(coords, x, y) => new Coords(
coords.x + x,
coords.y + y
)
)
let origin = new Coords(0, 0)
Ready to run.
Test | Ops/sec | |
---|---|---|
add singledispatch |
| ready |
add method |
| ready |
clone singledispatch |
| ready |
clone method |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.