-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.js
36 lines (30 loc) · 847 Bytes
/
replace.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
const fs = require('fs');
const timer = () => {
const start = new Date();
return {
stop: function () {
const end = new Date();
return end.getTime() - start.getTime();
}
}
};
const benchmarkReplace = () => {
fs.readFile('./loremIpsum', 'utf8', (err, data) => {
const t = timer('Using the replace method');
data.replace(/lorem/gmi, `lor`);
console.log(`Replace method: ${t.stop()}`);
});
}
const benchmarkSplit = () => {
fs.readFile('./loremIpsum', 'utf8', (err, data) => {
const t = timer('Using split and join');
data.split(/lorem/gmi).join(`lor`);
console.log(`Split and Join time: ${t.stop()}`);
});
}
// for (i = 1; i <= 10; i += 1) {
// benchmarkReplace();
// }
for (i = 1; i <= 10; i += 1) {
benchmarkSplit();
}