keys.js 6.11 KB
Newer Older
1
// Copyright 2016 the V8 project authors. All rights reserved.
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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

function ObjectWithKeys(count, keyOffset, keyGen) {
  if (keyOffset === undefined) keyOffset = 0;
  if (keyGen === undefined) keyGen = (i) => { return "key" + i };
  var o = {};
  for (var i = 0; i < count; i++) {
    var key = keyGen(i + keyOffset);
    o[key] = "value";
  }
  return o
}

function ObjectWithMixedKeys(count, keyOffset) {
  return ObjectWithKeys(count, keyOffset, (key) => {
    if (key % 2 == 0) return key;
    return "key" + key;
  });
}

// Create an object with #depth prototypes each having #keys properties.
function ObjectWithProtoKeys(depth, keys, cacheable) {
  var o = ObjectWithKeys(keys);
  var current = o;
  var keyOffset = 0;
  for (var i = 0; i < depth; i++) {
    keyOffset += keys;
    current.__proto__ = ObjectWithKeys(keys, keyOffset);
    current = current.__proto__;
  }
  if (cacheable === false) {
    // Add an empty proxy at the prototype chain to make caching properties
    // impossible.
    current.__proto__ = new Proxy({}, {});
  }
  return o;
}

function HoleyIntArray(size) {
  var array = new Array(size);
  for (var i = 0; i < size; i += 3) {
    array[i] = i;
  }
  return array
}

function IntArray(size) {
  var array = new Array(size);
  for (var i = 0; i < size; i++) {
    array[i] = i;
  }
  return array;
}

// ============================================================================
var object_empty = {};
var array_empty = [];

61 62 63 64
var array_int_50 = IntArray(50);
var array_int_50_proto_elements = IntArray(50);
array_int_50_proto_elements.__proto__ = [51, 52, 53, 54];
var array_int_holey_50 = HoleyIntArray(50);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

var empty_proto_5_10 = ObjectWithKeys(5);
empty_proto_5_10.__proto__ = ObjectWithProtoKeys(10, 0);

var empty_proto_5_5_slow = ObjectWithKeys(5);
empty_proto_5_5_slow.__proto__ = ObjectWithProtoKeys(5, 0, false);

var object_elements_proto_5_10 = ObjectWithKeys(5);
object_elements_proto_5_10.__proto__ = ObjectWithProtoKeys(10, 0);
// Add some properties further up the prototype chain, the rest stays
// empty.
for (var i = 0; i < 5; i++) {
  object_elements_proto_5_10.__proto__.__proto__.__proto__["proto" + i] = true;
}

var TestObjects = {
  object_empty: object_empty,
  array_empty: array_empty,
83 84 85
  array_int_50: array_int_50,
  array_int_holey_50: array_int_holey_50,
  array_int_50_proto_elements: array_int_50_proto_elements,
86 87 88 89 90
  empty_proto_5_10: empty_proto_5_10,
  empty_proto_5_5_slow: empty_proto_5_5_slow,
  object_elements_proto_5_10: object_elements_proto_5_10
}

91 92 93 94 95 96
var TestArrays = {
  array_empty: array_empty,
  array_int_50: array_int_50,
  array_int_holey_50: array_int_holey_50,
  array_int_50_proto_elements: array_int_50_proto_elements,
}
97 98 99 100 101 102 103 104 105 106 107 108 109

// ============================================================================

function CreateTestFunctionGen(fn) {
  // Force a new function for each test-object to avoid side-effects due to ICs.
  return (object) => {
    var random_comment = "\n// random comment" + Math.random() + "\n";
    return eval(random_comment + fn.toString());
  }
}

var TestFunctions = {
  "Object.keys()": CreateTestFunctionGen(() => {return Object.keys(object)}),
110
  "for-in": CreateTestFunctionGen(() => {
111 112 113 114 115 116 117 118
    var count = 0;
    var result;
    for (var key in object) {
      count++;
      result = object[key];
    };
    return [result, count];
  }),
119
  "for-in hasOwnProperty()": CreateTestFunctionGen(() => {
120 121 122 123 124 125 126 127 128 129 130 131
    var count = 0;
    var result;
    for (var key in object) {
      if (!object.hasOwnProperty(key)) continue;
      count++;
      result = object[key];
    };
    return [result, count];
  }),
  "for (i < Object.keys().length)": CreateTestFunctionGen(() => {
    var count = 0;
    var result;
132 133
    var keys = Object.keys(object);
    for (var i = 0; i < keys.length; i++) {
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
      count++;
      result = object[keys[i]];
    };
    return [result, count];
  }),
  "Object.keys().forEach()": CreateTestFunctionGen(() => {
    var count = 0;
    var result;
    Object.keys(object).forEach((value, index, obj) => {
      count++;
      result = value;
    });
    return [result, count];
  }),
}

var TestFunctionsArrays = {
  "for (i < array.length)": CreateTestFunctionGen(() => {
    var count = 0;
    var result;
    for (var i = 0; i < object.length; i++) {
      count++;
      result = object[i];
    };
    return [result, count];
  }),
  "for (i < length)": CreateTestFunctionGen(() => {
    var count = 0;
    var result;
    var length = object.length;
    for (var i = 0; i < length; i++) {
      count++;
      result = object[i];
    };
    return [result, count];
  })
}

// ============================================================================
173 174
// Create the benchmark suites. We create a suite for each of the test
// functions above and each suite contains benchmarks for each object type.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
var Benchmarks = [];

function NewBenchmark(
    test_function_gen, test_function_name, test_object, test_object_name) {
  var object = test_object;
  var name = test_function_name + " " + test_object_name;
  var test_function = test_function_gen(object);
  return new Benchmark(name, false, false, 0, test_function)
}

for (var test_function_name in TestFunctions) {
  var test_function_gen = TestFunctions[test_function_name];
  var benchmarks = [];
  for (var test_object_name in TestObjects) {
    var test_object = TestObjects[test_object_name];
    var benchmark = NewBenchmark(
        test_function_gen, test_function_name, test_object, test_object_name);
    benchmarks.push(benchmark);
  }
194
  Benchmarks.push(new BenchmarkSuite(test_function_name, [100], benchmarks));
195 196 197 198 199 200 201 202 203 204 205
}

for (var test_function_name in TestFunctionsArrays) {
  var test_function_gen = TestFunctionsArrays[test_function_name];
  var benchmarks = [];
  for (var test_array_name in TestArrays) {
    var test_array = TestArrays[test_array_name];
    var benchmark = NewBenchmark(
        test_function_gen, test_function_name, test_array, test_array_name);
    benchmarks.push(benchmark);
  }
206
  Benchmarks.push(new BenchmarkSuite(test_function_name, [100], benchmarks));
207 208 209
}

// ============================================================================