/
constructor.bench.js
64 lines (56 loc) · 1.54 KB
/
constructor.bench.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Bench } from '/node_modules/tinybench/dist/index.js';
const felix = { name: 'felix', lives: 9 };
class MyClass {
constructor({ name, lives }) {
this.name = name;
this.lives = lives;
}
}
const myObject = { ...felix };
function myFunction({ name, lives }) {
return { name, lives };
}
function thisFunction({ name, lives }) {
this.name = name;
this.lives = lives;
}
const benchmark = true;
if(benchmark) {
await doBench(create_objects);
}
async function create_objects(bench) {
bench
.add('new class', () => {
new MyClass(felix);
})
.add('function call', () => {
myFunction(felix);
})
.add('new function', () => {
// eslint-disable-next-line new-cap
new myFunction(felix);
})
.add('new this function', () => {
// eslint-disable-next-line new-cap
new thisFunction(felix);
})
.add('Reflect.construct', () => {
const thi$ = Object.create(null);
thisFunction.call(thi$, felix);
});
}
async function doBench(setup) {
const bench = new Bench({ time: 200 });
setup(bench);
await bench.warmup(); // make results more reliable, ref: https://github.com/tinylibs/tinybench/pull/50
await bench.run();
const tabulation = bench.table();
console.table(tabulation);
if(bench.todos.length) {
console.table(
bench.todos.map(({ name }) => ({
'Task name': name,
})),
);
}
}