diff options
author | Vse Mozhet Byt <vsemozhetbyt@gmail.com> | 2017-04-03 00:32:50 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-04-04 09:19:18 -0700 |
commit | 74dc3bfe08b8f0a47759206d3d9d4d6f5a0528c1 (patch) | |
tree | 11c56785a6c9b4331314eee66f1dca0fff8b3176 /benchmark/es | |
parent | 9348f31c2aec996cf6cf7731244b2d76153440ea (diff) | |
download | android-node-v8-74dc3bfe08b8f0a47759206d3d9d4d6f5a0528c1.tar.gz android-node-v8-74dc3bfe08b8f0a47759206d3d9d4d6f5a0528c1.tar.bz2 android-node-v8-74dc3bfe08b8f0a47759206d3d9d4d6f5a0528c1.zip |
benchmark: replace [].join() with ''.repeat()
Also add a benchmark to compare both ways to create strings.
PR-URL: https://github.com/nodejs/node/pull/12170
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'benchmark/es')
-rw-r--r-- | benchmark/es/string-repeat.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/benchmark/es/string-repeat.js b/benchmark/es/string-repeat.js new file mode 100644 index 0000000000..a6b389033a --- /dev/null +++ b/benchmark/es/string-repeat.js @@ -0,0 +1,35 @@ +'use strict'; + +const assert = require('assert'); +const common = require('../common.js'); + +const configs = { + n: [1e3], + mode: ['Array', 'repeat'], + encoding: ['ascii', 'utf8'], + size: [1e1, 1e3, 1e6], +}; + +const bench = common.createBenchmark(main, configs); + +function main(conf) { + const n = +conf.n; + const size = +conf.size; + const character = conf.encoding === 'ascii' ? 'a' : '\ud83d\udc0e'; // '🐎' + + let str; + + if (conf.mode === 'Array') { + bench.start(); + for (let i = 0; i < n; i++) + str = new Array(size + 1).join(character); + bench.end(n); + } else { + bench.start(); + for (let i = 0; i < n; i++) + str = character.repeat(size); + bench.end(n); + } + + assert.strictEqual([...str].length, size); +} |