summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnatoli Papirovski <apapirovski@mac.com>2018-05-01 14:22:39 +0200
committerAnatoli Papirovski <apapirovski@mac.com>2018-05-03 14:47:17 +0200
commitb04d0921aeaa70521dea5fcb27256c686157eb87 (patch)
tree319a20914c96838482c937d79f830c8fc51abb38
parentfe8794560a22cc62a482bf071e2050ce1ea6ea3e (diff)
downloadnode-new-b04d0921aeaa70521dea5fcb27256c686157eb87.tar.gz
util: improve spliceOne perf
Do less variable allocations and reassignments inside spliceOne since it's relied on by some performance sensitive code. PR-URL: https://github.com/nodejs/node/pull/20453 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
-rw-r--r--benchmark/util/splice-one.js33
-rw-r--r--lib/internal/util.js7
-rw-r--r--test/parallel/test-benchmark-util.js2
3 files changed, 39 insertions, 3 deletions
diff --git a/benchmark/util/splice-one.js b/benchmark/util/splice-one.js
new file mode 100644
index 0000000000..5c2a39f6d7
--- /dev/null
+++ b/benchmark/util/splice-one.js
@@ -0,0 +1,33 @@
+'use strict';
+
+const common = require('../common');
+
+const bench = common.createBenchmark(main, {
+ n: [1e7],
+ pos: ['start', 'middle', 'end'],
+ size: [10, 100, 500],
+}, { flags: ['--expose-internals'] });
+
+function main({ n, pos, size }) {
+ const { spliceOne } = require('internal/util');
+ const arr = new Array(size);
+ arr.fill('');
+ let index;
+ switch (pos) {
+ case 'end':
+ index = size - 1;
+ break;
+ case 'middle':
+ index = Math.floor(size / 2);
+ break;
+ default: // start
+ index = 0;
+ }
+
+ bench.start();
+ for (var i = 0; i < n; i++) {
+ spliceOne(arr, index);
+ arr.push('');
+ }
+ bench.end(n);
+}
diff --git a/lib/internal/util.js b/lib/internal/util.js
index 071563a737..07515e2e09 100644
--- a/lib/internal/util.js
+++ b/lib/internal/util.js
@@ -322,10 +322,11 @@ function join(output, separator) {
return str;
}
-// About 1.5x faster than the two-arg version of Array#splice().
+// As of V8 6.6, depending on the size of the array, this is anywhere
+// between 1.5-10x faster than the two-arg version of Array#splice()
function spliceOne(list, index) {
- for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
- list[i] = list[k];
+ for (; index + 1 < list.length; index++)
+ list[index] = list[index + 1];
list.pop();
}
diff --git a/test/parallel/test-benchmark-util.js b/test/parallel/test-benchmark-util.js
index 9a6ae370b7..838e51daac 100644
--- a/test/parallel/test-benchmark-util.js
+++ b/test/parallel/test-benchmark-util.js
@@ -10,6 +10,8 @@ runBenchmark('util',
'method=Array',
'n=1',
'option=none',
+ 'pos=start',
+ 'size=1',
'type=',
'version=native'],
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });