/
index.mjs
87 lines (77 loc) · 1.88 KB
/
index.mjs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import Benchmark from 'benchmark';
import MagicString from '../dist/magic-string.es.mjs';
import fs from 'fs/promises';
Benchmark.support.decompilation = false;
console.log(`node ${process.version}\n`);
function runWithInstance(name, inputs, func, setup) {
const ss = [];
return new Benchmark(
name,
{
setup: () => {
for (const [i, input] of inputs.entries()) {
ss[i] = new MagicString(input);
if (setup) {
setup(ss[i]);
}
}
},
fn: () => {
for (const i of inputs.keys()) {
func(ss[i]);
}
}
}
).on('complete', (event) => {
console.log(String(event.target));
}).on('error', (event) => {
console.error(event.target.error);
}).run();
}
async function bench() {
const inputs = await Promise.all(
['data.js', 'data-min.js'].map(
(file) => fs.readFile(new URL(file, import.meta.url), 'utf-8')
)
);
new Benchmark('construct', {
fn: () => {
for (const input of inputs) {
new MagicString(input);
}
}
}).on('complete', (event) => {
console.log(String(event.target));
}).on('error', (event) => {
console.error(event.target.error);
}).run()
runWithInstance('append', inputs, s => {
s.append(';"append";');
});
runWithInstance('indent', inputs, s => {
s.indent();
});
runWithInstance('generateMap (no edit)', inputs, s => {
s.generateMap();
});
runWithInstance('generateMap (edit)', inputs, s => {
s.generateMap();
}, s => {
s.replace(/replacement/g, 'replacement\nReplacement');
});
runWithInstance('generateDecodedMap (no edit)', inputs, s => {
s.generateDecodedMap();
});
runWithInstance('generateDecodedMap (edit)', inputs, s => {
s.generateDecodedMap();
}, s => {
s.replace(/replacement/g, 'replacement\nReplacement');
});
const size = 1000000;
runWithInstance('overwrite', ['a'.repeat(size)], s => {
for (let i = 1; i < size; i+=2) {
s.overwrite(i, i+1, 'b');
}
});
}
bench();