Skip to content

Commit 03b12ba

Browse files
authored
Init
0 parents  commit 03b12ba

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

index.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
// Load Chance
3+
var Chance = require('chance');
4+
// Instantiate Chance so it can be used
5+
var chance = new Chance();
6+
var moment = require('moment');
7+
/**
8+
* The sentence we will be manipulating
9+
* Sometimes it is better to just walk away from things and go back to them later when you’re in a better frame of mind.
10+
*/
11+
12+
(() => {
13+
// Use the same word list for all tests to make it fair
14+
let wordList = buildWordList();
15+
16+
// Test 1
17+
let startTest1 = moment();
18+
for(let i = 0; i < 1000000; i++){
19+
sringTemplates(wordList);
20+
}
21+
let endTest1 = moment();
22+
let test1duration = moment.duration(endTest1.diff(startTest1));
23+
let test1Seconds = test1duration.asSeconds();
24+
25+
// Test 2
26+
let startTest2 = moment();
27+
for(let i = 0; i < 1000000; i++){
28+
singleQuotesWithStringBuildup(wordList);
29+
}
30+
let endTest2 = moment();
31+
let test2duration = moment.duration(endTest2.diff(startTest2));
32+
let test2Seconds = test2duration.asSeconds();
33+
34+
// Test 3
35+
let startTest3 = moment();
36+
for(let i = 0; i < 1000000; i++){
37+
replaceMultipleTimes(wordList);
38+
}
39+
let endTest3 = moment();
40+
let test3duration = moment.duration(endTest3.diff(startTest3));
41+
let test3Seconds = test3duration.asSeconds();
42+
43+
console.log("test 1 time", test1Seconds);
44+
console.log("test 2 time", test2Seconds);
45+
console.log("test 3 time", test3Seconds);
46+
})();
47+
48+
function sringTemplates(wordList){
49+
let template = `Sometimes ${wordList[0]} it ${wordList[1]} is ${wordList[2]} better ${wordList[3]} to ${wordList[4]} just ${wordList[5]} walk ${wordList[6]} away ${wordList[7]} from ${wordList[8]} things ${wordList[9]} and ${wordList[10]} go ${wordList[11]} back ${wordList[12]} to ${wordList[13]} them ${wordList[4]} later ${wordList[5]} when ${wordList[16]} you’re ${wordList[17]} in ${wordList[18]} a ${wordList[19]} better ${wordList[20]} frame ${wordList[21]} of ${wordList[22]} mind.`;
50+
}
51+
52+
function singleQuotesWithStringBuildup(wordList){
53+
let template = 'Sometimes ' + wordList[0] + ' it ' + wordList[1] + ' is ' + wordList[2] + ' better ' + wordList[3] + ' to ' + wordList[4] + ' just ' + wordList[5] + ' walk ' + wordList[6] + ' away ' + wordList[7] + ' from ' + wordList[8] + ' things ' + wordList[9] + ' and ' + wordList[10] + ' go ' + wordList[11] + ' back ' + wordList[12] + ' to ' + wordList[13] + ' them ' + wordList[14] + ' later ' + wordList[15] + ' when ' + wordList[16] + ' you’re ' + wordList[17] + ' in ' + wordList[18] + ' a ' + wordList[19] + ' better ' + wordList[20] + ' frame ' + wordList[21] + ' of ' + wordList[22] + ' mind.';
54+
}
55+
56+
function replaceMultipleTimes(wordList){
57+
// Could use: [str0], [str1], [str2] etc, however how to you increment without using '[str'+i+']' and skew the results?
58+
let template = 'Sometimes [str] it [str] is [str] better [str] to [str] just [str] walk [str] away [str] from [str] things [str] and [str] go [str] back [str] to [str] them [str] later [str] when [str] you’re [str] in [str] a [str] better [str] frame [str] of [str] mind.';
59+
for(let i = 0; i < 24; i++){
60+
// Only use replace here, as we want to replace the first one, and then replace the next one on the next loop with the next word.
61+
template = template.replace('[str]', wordList[i]);
62+
}
63+
}
64+
65+
function buildWordList(){
66+
let arr = [];
67+
for(let i = 0; i < 24; i++){
68+
arr.push(chance.string({ length: 5 }));
69+
}
70+
return arr;
71+
}

package-lock.json

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "js-string-benchmarking",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"chance": "^1.1.4",
13+
"moment": "^2.24.0"
14+
}
15+
}

0 commit comments

Comments
 (0)