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
https://stackoverflow.com/questions/78362457/is-there-a-general-approach-for-optimizing-bitwise-expressions-that-distinguish < trying some of the solutions here, along with some more naive solutions.
const isN = [
false, true, false,
true, true,
false, true, false,
];
function run (test) {
let guys = 0;
for (let y = 0; y < 100; y++) {
for (let x = 0; x < 100; x++) {
for (let i = 0; i < 8; i++) {
const n = isN[i];
if (Boolean(test(i)) !== n) {
throw new Error(`wrong: ${test(i)} !== ${n} @ ${x},${y}`);
}
guys++;
}
}
}
console.log(`tested ${guys} guys ok`);
}
// share the load mr frodo
const LUT = [0, 1, 0, 1, 1, 0, 1, 0];
const BYEAHS = new Set([1,3,4,6])
const MAP = new Map([
[0, false],
[1, true],
[2, false],
[3, true],
[4, true],
[5, false],
[6, true],
[7, false],
]);
const OBJ = {
0: false,
1: true,
2: false,
3: true,
4: true,
5: false,
6: true,
7: false,
};
const NPO = Object.create(null, {
0: { value: false },
1: { value: true },
2: { value: false },
3: { value: true },
4: { value: true },
5: { value: false },
6: { value: true },
7: { value: false },
})
const GuyEvaluatorFactory = (() => {
class GuyError extends Error {
name = 'GuyError'
constructor () {
super('what am i doing with my life');
}
}
const _GuyEvaluatorImpl = class {
/** @prop - try to cache instances to help performance? TODO - hook up debug tooling...
*/
static _instances = [];
/**
* @prop 0|1|2|3|4|5|6|7 x: the number whose guy status is being memoized
* @prop boolean isGuy: whether x is a guy... TODO - we really need typescript, this is not maintainable
*/
/**
* @param boolean isGuy: whether the instance being created is a guy
*/
constructor (isGuy) {
if (isGuy == null)
throw new GuyError();
this.isGuy = isGuy;
}
/**
* @param ok im tired of the joke now
* @returns GuyEvaluator
*/
static getOrCreateGuyEvaluator (x, isGuy) {
return this._instances[x] ??= new this(isGuy)
}
static testGuy(x) {
return this
.getOrCreateGuyEvaluator(x).isGuy;
}
static initSingleton() {
this.getOrCreateGuyEvaluator(0, false);
this.getOrCreateGuyEvaluator(1, true);
this.getOrCreateGuyEvaluator(2, false);
this.getOrCreateGuyEvaluator(3, true);
this.getOrCreateGuyEvaluator(4, true);
this.getOrCreateGuyEvaluator(5, false);
this.getOrCreateGuyEvaluator(6, true);
this.getOrCreateGuyEvaluator(7, false);
return this;
}
}
return _GuyEvaluatorImpl
.initSingleton()
.testGuy
.bind(_GuyEvaluatorImpl);
})();
Ready to run.
Test | Ops/sec | |
---|---|---|
chained boolean `||` (reference implementation) |
| ready |
switch |
| ready |
smallbrain test 1 |
| ready |
1st answer |
| ready |
2nd answer |
| ready |
3rd answer |
| ready |
LUT |
| ready |
set |
| ready |
object, lol |
| ready |
TRYHARD object |
| ready |
map? |
| ready |
Enterprise guy solution |
| ready |
the humble if herd |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.