-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: add benchmark for string concatenations
PR-URL: #12455 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
08ba9d4
commit 7d87edc
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
|
||
const configs = { | ||
n: [1e3], | ||
mode: [ | ||
'multi-concat', | ||
'multi-join', | ||
'multi-template', | ||
'to-string-string', | ||
'to-string-concat', | ||
'to-string-template', | ||
], | ||
}; | ||
|
||
const bench = common.createBenchmark(main, configs); | ||
|
||
|
||
function main(conf) { | ||
const n = +conf.n; | ||
const mode = conf.mode; | ||
|
||
const str = 'abc'; | ||
const num = 123; | ||
|
||
let string; | ||
|
||
switch (mode) { | ||
case 'multi-concat': | ||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
string = '...' + str + ', ' + num + ', ' + str + ', ' + num + '.'; | ||
bench.end(n); | ||
break; | ||
case 'multi-join': | ||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
string = ['...', str, ', ', num, ', ', str, ', ', num, '.'].join(''); | ||
bench.end(n); | ||
break; | ||
case 'multi-template': | ||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
string = `...${str}, ${num}, ${str}, ${num}.`; | ||
bench.end(n); | ||
break; | ||
case 'to-string-string': | ||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
string = String(num); | ||
bench.end(n); | ||
break; | ||
case 'to-string-concat': | ||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
string = '' + num; | ||
bench.end(n); | ||
break; | ||
case 'to-string-template': | ||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
string = `${num}`; | ||
bench.end(n); | ||
break; | ||
} | ||
|
||
return string; | ||
} |