Skip to content

Commit

Permalink
Fix benchmarks (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 authored May 18, 2024
1 parent 15a180f commit a9cc1e2
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 325 deletions.
147 changes: 76 additions & 71 deletions bench/array.bench.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/* globals suite benchmark */
import {benchSettings} from 'karma-webpack-bundle';
import onChange from '../index.js';
import {suite} from './utils.js';

let temporaryTarget; // eslint-disable-line no-unused-vars
const callback = function () {};
let array = [];
let value = 0;

const buildSettings = before => ({
...benchSettings,
onStart: before,
onCycle: before,
});
const buildArray = length => Array.from({length})
.fill(0)
.map((value, index) => ({a: index}));

const sizes = [{
size: 10,
Expand All @@ -21,126 +17,135 @@ const sizes = [{
name: 'large',
}];

const commonBench = bench => {
const commonBench = (bench, objectCallback) => {
for (const [index, option] of sizes.entries()) {
const separator = (index === sizes.length - 1)
? ''
: ' ' + '_'.repeat(20 - option.name.length);

benchmark(`(${option.name}) no options`, bench, buildSettings(() => {
array = onChange(buildArray(option.size), callback);
}));

benchmark(`(${option.name}) isShallow`, bench, buildSettings(() => {
array = onChange(buildArray(option.size), callback, {isShallow: true});
}));

benchmark(`(${option.name}) pathAsArray`, bench, buildSettings(() => {
array = onChange(buildArray(option.size), callback, {pathAsArray: true});
}));

benchmark(`(${option.name}) empty Proxy`, bench, buildSettings(() => {
array = new Proxy(buildArray(option.size), {});
}));
const taskOptions = options => ({
beforeAll() {
this.o = onChange(buildArray(option.size), callback, options);
},
afterAll() {
delete this.o;
},
});

benchmark(`(${option.name}) native ${separator}`, bench, buildSettings(() => {
array = buildArray(option.size);
}));
const simpleTaskOptions = {
beforeAll() {
this.o = buildArray(option.size);
},
afterAll() {
delete this.o;
},
};

bench
.add(`(${option.name}) no options`, function () {
objectCallback(this.o);
}, taskOptions())
.add(`(${option.name}) isShallow`, function () {
objectCallback(this.o);
}, taskOptions({isShallow: true}))
.add(`(${option.name}) pathAsArray`, function () {
objectCallback(this.o);
}, taskOptions({pathAsArray: true}))
.add(`(${option.name}) empty Proxy`, function () {
objectCallback(new Proxy(this.o, {}));
}, simpleTaskOptions)
.add(`(${option.name}) native ${separator}`, function () {
objectCallback(this.o);
}, simpleTaskOptions);
}
};

const buildArray = length => Array.from({length})
.fill(0)
.map((value, index) => ({a: index}));

suite('on-change init with array', () => {
array = buildArray(sizes[0].size);

benchmark('new Proxy', () => {
temporaryTarget = new Proxy(array, {});
}, benchSettings);

benchmark('no options', () => {
onChange(array, callback);
}, benchSettings);

benchmark('pathAsArray', () => {
onChange(array, callback, {pathAsArray: true});
}, benchSettings);

benchmark('fat-arrow', () => {
onChange(array, () => {});
}, benchSettings);
await suite('on-change init with array', bench => {
const array = buildArray(sizes[0].size);

bench
.add('new Proxy', () => {
temporaryTarget = new Proxy(array, {});
})
.add('no options', () => {
onChange(array, callback);
})
.add('pathAsArray', () => {
onChange(array, callback, {pathAsArray: true});
})
.add('fat-arrow', () => {
onChange(array, () => {});
});
});

suite('on-change with array, read', () => {
commonBench(() => {
await suite('on-change with array, read', bench => {
commonBench(bench, array => {
temporaryTarget = array[3];
});
});

suite('on-change with array, read nested', () => {
commonBench(() => {
await suite('on-change with array, read nested', bench => {
commonBench(bench, array => {
temporaryTarget = array[3].a;
});
});

suite('on-change with array, write', () => {
commonBench(() => {
await suite('on-change with array, write', bench => {
commonBench(bench, array => {
array[2] = value++;
});
});

suite('on-change with array, write nested', () => {
commonBench(() => {
await suite('on-change with array, write nested', bench => {
commonBench(bench, array => {
array[4].a = value++;
});
});

suite('on-change with array, read in apply', () => {
commonBench(() => {
await suite('on-change with array, read in apply', bench => {
commonBench(bench, array => {
array.some((value, index) => {
temporaryTarget = array[index];
return true;
});
});
});

suite('on-change with array, write in apply', () => {
commonBench(() => {
await suite('on-change with array, write in apply', bench => {
commonBench(bench, array => {
array.some((value, index) => {
array[index] = value++;
return true;
});
});
});

suite('on-change with array, push', () => {
commonBench(() => {
await suite('on-change with array, push', bench => {
commonBench(bench, array => {
array.push(0);
});
});

suite('on-change with array, pop', () => {
commonBench(() => {
await suite('on-change with array, pop', bench => {
commonBench(bench, array => {
array.pop();
});
});

suite('on-change with array, unshift', () => {
commonBench(() => {
await suite('on-change with array, unshift', bench => {
commonBench(bench, array => {
array.unshift(0);
});
});

suite('on-change with array, shift', () => {
commonBench(() => {
await suite('on-change with array, shift', bench => {
commonBench(bench, array => {
array.shift();
});
});

suite('on-change with array, toString', () => {
commonBench(() => {
await suite('on-change with array, toString', bench => {
commonBench(bench, array => {
temporaryTarget = array.toString();
});
});
21 changes: 10 additions & 11 deletions bench/cache.bench.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/* globals suite benchmark */
import {benchSettings} from 'karma-webpack-bundle';
import Cache from '../lib/cache.js';
import {suite} from './utils.js';

let temporaryTarget;

suite('Cache init', () => {
benchmark('init', () => {
temporaryTarget = new Cache();
}, benchSettings);

benchmark('getProxy', () => {
temporaryTarget = new Cache();
temporaryTarget.getProxy({}, '', {});
}, benchSettings);
await suite('Cache init', bench => {
bench
.add('init', () => {
temporaryTarget = new Cache();
})
.add('getProxy', () => {
temporaryTarget = new Cache();
temporaryTarget.getProxy({}, '', {});
});
});
6 changes: 6 additions & 0 deletions bench/index.bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
await import('./main.bench.js');
await import('./array.bench.js');
await import('./cache.bench.js');
await import('./is-builtin.bench.js');
await import('./path.bench.js');
await import('./smart-clone.bench.js');
56 changes: 26 additions & 30 deletions bench/is-builtin.bench.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
/* globals suite benchmark */
import {benchSettings} from 'karma-webpack-bundle';
import {isBuiltinWithMutableMethods, isBuiltinWithoutMutableMethods} from '../lib/is-builtin.js';
import {suite} from './utils.js';

let temporaryTarget; // eslint-disable-line no-unused-vars

suite('isBuiltinWithMutableMethods', () => {
await suite('isBuiltinWithMutableMethods', bench => {
const date = new Date();
const notDate = 'a';

benchmark('date', () => {
temporaryTarget = isBuiltinWithMutableMethods(date);
}, benchSettings);

benchmark('not date', () => {
temporaryTarget = isBuiltinWithMutableMethods(notDate);
}, benchSettings);
bench
.add('date', () => {
temporaryTarget = isBuiltinWithMutableMethods(date);
})
.add('not date', () => {
temporaryTarget = isBuiltinWithMutableMethods(notDate);
});
});

suite('isBuiltinWithoutMutableMethods', () => {
await suite('isBuiltinWithoutMutableMethods', bench => {
const testNaN = Number.NaN;
const testRegExp = /as/g;
const testString = 'a';
const testNumber = 42;
const testNumberInstance = Number(42);

benchmark('NaN', () => {
temporaryTarget = isBuiltinWithoutMutableMethods(testNaN);
}, benchSettings);

benchmark('regexp', () => {
temporaryTarget = isBuiltinWithoutMutableMethods(testRegExp);
}, benchSettings);

benchmark('string', () => {
temporaryTarget = isBuiltinWithoutMutableMethods(testString);
}, benchSettings);

benchmark('number', () => {
temporaryTarget = isBuiltinWithoutMutableMethods(testNumber);
}, benchSettings);

benchmark('number instance', () => {
temporaryTarget = isBuiltinWithoutMutableMethods(testNumberInstance);
}, benchSettings);
bench
.add('NaN', () => {
temporaryTarget = isBuiltinWithoutMutableMethods(testNaN);
})
.add('regexp', () => {
temporaryTarget = isBuiltinWithoutMutableMethods(testRegExp);
})
.add('string', () => {
temporaryTarget = isBuiltinWithoutMutableMethods(testString);
})
.add('number', () => {
temporaryTarget = isBuiltinWithoutMutableMethods(testNumber);
})
.add('number instance', () => {
temporaryTarget = isBuiltinWithoutMutableMethods(testNumberInstance);
});
});
Loading

0 comments on commit a9cc1e2

Please sign in to comment.