run.js 3.35 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 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.

load('../base.js');

7 8 9 10 11 12 13 14 15
let array;
// Initialize func variable to ensure the first test doesn't benefit from
// global object property tracking.
let func = 0;
let this_arg;
let result;
const array_size = 100;
const max_index = array_size - 1;

16
// newClosure is a handy function to get a fresh
17 18
// closure unpolluted by IC feedback for a 2nd-order array builtin
// test.
19
function newClosure(name, generic = false) {
20 21 22 23 24 25 26
  if (generic) {
    return new Function(
      `result = Array.prototype.${name}.call(array, func, this_arg);`);
  }
  return new Function(`result = array.${name}(func, this_arg);`);
}

27 28 29 30 31 32 33
function MakeHoley(array) {
  for (let i =0; i < array.length; i+=2) {
    delete array[i];
  }
  assert(%HasHoleyElements(array));
}

34 35
function SmiSetup() {
  array = Array.from({ length: array_size }, (_, i) => i);
36 37 38 39 40 41 42
  assert(%HasSmiElements(array));
}

function HoleySmiSetup() {
  SmiSetup();
  MakeHoley(array);
  assert(%HasSmiElements(array));
43 44 45 46
}

function DoubleSetup() {
  array = Array.from({ length: array_size }, (_, i) => i + 0.5);
47 48 49 50 51 52 53
  assert(%HasDoubleElements(array));
}

function HoleyDoubleSetup() {
  DoubleSetup();
  MakeHoley(array);
  assert(%HasDoubleElements(array));
54 55 56 57
}

function FastSetup() {
  array = Array.from({ length: array_size }, (_, i) => `value ${i}`);
58
  assert(%HasObjectElements(array));
59 60 61 62 63
}

function HoleyFastSetup() {
  FastSetup();
  MakeHoley(array);
64
  assert(%HasObjectElements(array));
65 66
}

67
function DictionarySetup() {
68 69
  array = [];
  // Add a large index to force dictionary elements.
70 71 72 73
  array[2**30] = 10;
  // Spread out {array_size} elements.
  for (var i = 0; i < array_size-1; i++) {
    array[i*101] = i;
74 75
  }
  assert(%HasDictionaryElements(array));
76 77 78 79 80 81 82
}

function ObjectSetup() {
  array = { length: array_size };
  for (var i = 0; i < array_size; i++) {
    array[i] = i;
  }
83 84 85 86 87 88 89 90 91 92 93 94 95
  assert(%HasObjectElements(array));
  assert(%HasHoleyElements(array));
}


const ARRAY_SETUP = {
  PACKED_SMI: SmiSetup,
  HOLEY_SMI: HoleySmiSetup,
  PACKED_DOUBLE: DoubleSetup,
  HOLEY_DOUBLE: HoleyDoubleSetup,
  PACKED: FastSetup,
  HOLEY: HoleyFastSetup,
  DICTIONARY: DictionarySetup,
96 97 98 99 100
}

function DefineHigherOrderTests(tests) {
  let i = 0;
  while (i < tests.length) {
101
     const [name, testFunc, setupFunc, callback] = tests[i++];
102 103 104 105 106 107

     let setupFuncWrapper = () => {
       func = callback;
       this_arg = undefined;
       setupFunc();
     };
108
     createSuite(name, 1000, testFunc, setupFuncWrapper);
109 110 111 112
  }
}

// Higher-order Array builtins.
113
load('filter.js');
114
load('map.js');
115 116
load('every.js');
load('some.js');
117
load('for-each.js');
118 119
load('reduce.js');
load('reduce-right.js');
120
load('find.js');
121
load('find-index.js');
122

123
// Other Array builtins.
124 125
load('from.js');
load('of.js');
126 127
load('join.js');
load('to-string.js');
128
load('slice.js');
129
load('copy-within.js');
130

131 132 133 134 135 136
var success = true;

function PrintResult(name, result) {
  print(name + '-Array(Score): ' + result);
}

137
function PrintStep(name) {}
138 139 140 141 142 143 144 145 146 147 148 149 150

function PrintError(name, error) {
  PrintResult(name, error);
  success = false;
}


BenchmarkSuite.config.doWarmup = undefined;
BenchmarkSuite.config.doDeterministic = undefined;

BenchmarkSuite.RunSuites({ NotifyResult: PrintResult,
                           NotifyError: PrintError,
                           NotifyStep: PrintStep });