8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 58a65d6 commit e854c95Copy full SHA for e854c95
benchmark/util/splice-one.js
@@ -0,0 +1,33 @@
1
+'use strict';
2
+
3
+const common = require('../common');
4
5
+const bench = common 8000 .createBenchmark(main, {
6
+ n: [1e7],
7
+ pos: ['start', 'middle', 'end'],
8
+ size: [10, 100, 500],
9
+}, { flags: ['--expose-internals'] });
10
11
+function main({ n, pos, size }) {
12
+ const { spliceOne } = require('internal/util');
13
+ const arr = new Array(size);
14
+ arr.fill('');
15
+ let index;
16
+ switch (pos) {
17
+ case 'end':
18
+ index = size - 1;
19
+ break;
20
+ case 'middle':
21
+ index = Math.floor(size / 2);
22
23
+ default: // start
24
+ index = 0;
25
+ }
26
27
+ bench.start();
28
+ for (var i = 0; i < n; i++) {
29
+ spliceOne(arr, index);
30
+ arr.push('');
31
32
+ bench.end(n);
33
+}
lib/internal/util.js
@@ -322,10 +322,11 @@ function join(output, separator) {
322
return str;
323
}
324
325
-// 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
326
+// between 1.5-10x faster than the two-arg version of Array#splice()
327
function spliceOne(list, index) {
- for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
328
- list[i] = list[k];
+ for (; index + 1 < list.length; index++)
329
+ list[index] = list[index + 1];
330
list.pop();
331
332
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 });