slice-perf.js 2.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

const kIterations = 1000000;
const kIterationShort = 10000;
const kArraySize = 64;

let smi_array = [];
for (let i = 0; i < kArraySize; ++i) smi_array[i] = Math.floor(Math.random() * 100);

let start = performance.now();
for (let x = 0; x < kIterations; ++x) {
  smi_array.slice(0);
}
let stop = performance.now();
print("smi_array copy: " + (Math.floor((stop - start)*10)/10) + " ms");

start = performance.now();
for (let x = 0; x < kIterations; ++x) {
  smi_array.slice(x % kArraySize);
}
stop = performance.now();
print("smi_array: " + (Math.floor((stop - start)*10)/10) + " ms");

let double_array = [];
for (let i = 0; i < kArraySize; ++i) double_array[i] = Math.random() * 100;
start = performance.now();
for (let x = 0; x < kIterations; ++x) {
  double_array.slice(x % kArraySize);
}
stop = performance.now();
print("double_array: " + (Math.floor((stop - start)*10)/10) + " ms");

let object_array = [];
for (let i = 0; i < kArraySize; ++i) object_array[i] = new Object();
start = performance.now();
for (let x = 0; x < kIterations; ++x) {
  object_array.slice(x % kArraySize);
}
stop = performance.now();
print("object_array: " + (Math.floor((stop - start)*10)/10) + " ms");

let dictionary_array = [];
for (let i = 0; i < kArraySize; ++i) dictionary_array[i] = new Object();
dictionary_array[100000] = new Object();
start = performance.now();
for (let x = 0; x < kIterationShort; ++x) {
  dictionary_array.slice(x % kArraySize);
}
stop = performance.now();
print("dictionary: " + (Math.floor((stop - start)*10)/10) + " ms");

let arguments_array;
function sloppy() {
  arguments_array = arguments;
}
sloppy.apply(null, smi_array);
start = performance.now();
for (let x = 0; x < kIterations; ++x) {
  let r = Array.prototype.slice.call(arguments_array, x % kArraySize);
}
stop = performance.now();
print("arguments_array (sloppy): " + (Math.floor((stop - start)*10)/10) + " ms");

function sloppy2 (a) {
  arguments_array = arguments;
}
sloppy2.apply(null, smi_array);
start = performance.now();
for (let x = 0; x < kIterations; ++x) {
  Array.prototype.slice.call(arguments_array, x % kArraySize);
}
stop = performance.now();
print("arguments_array (fast aliased): " + (Math.floor((stop - start)*10)/10) + " ms");

delete arguments_array[5];
start = performance.now();
for (let x = 0; x < kIterationShort; ++x) {
  Array.prototype.slice.call(arguments_array, x % kArraySize);
}
stop = performance.now();
print("arguments_array (slow aliased): " + (Math.floor((stop - start)*10)/10) + " ms");