map.js 4.44 KB
Newer Older
1 2 3 4 5
// 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.


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
var MapSmiBenchmark = new BenchmarkSuite('Map-Smi', [1000], [
  new Benchmark('Set', false, false, 0, MapSetSmi, MapSetupSmiBase, MapTearDown),
  new Benchmark('Has', false, false, 0, MapHasSmi, MapSetupSmi, MapTearDown),
  new Benchmark('Get', false, false, 0, MapGetSmi, MapSetupSmi, MapTearDown),
  new Benchmark('Delete', false, false, 0, MapDeleteSmi, MapSetupSmi, MapTearDown),
]);


var MapStringBenchmark = new BenchmarkSuite('Map-String', [1000], [
  new Benchmark('Set', false, false, 0, MapSetString, MapSetupStringBase, MapTearDown),
  new Benchmark('Has', false, false, 0, MapHasString, MapSetupString, MapTearDown),
  new Benchmark('Get', false, false, 0, MapGetString, MapSetupString, MapTearDown),
  new Benchmark('Delete', false, false, 0, MapDeleteString, MapSetupString, MapTearDown),
]);


var MapObjectBenchmark = new BenchmarkSuite('Map-Object', [1000], [
  new Benchmark('Set', false, false, 0, MapSetObject, MapSetupObjectBase, MapTearDown),
  new Benchmark('Has', false, false, 0, MapHasObject, MapSetupObject, MapTearDown),
  new Benchmark('Get', false, false, 0, MapGetObject, MapSetupObject, MapTearDown),
  new Benchmark('Delete', false, false, 0, MapDeleteObject, MapSetupObject, MapTearDown),
]);


var MapIterationBenchmark = new BenchmarkSuite('Map-Iteration', [1000], [
  new Benchmark('ForEach', false, false, 0, MapForEach, MapSetupSmi, MapTearDown),
32 33 34 35 36 37
]);


var map;


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
function MapSetupSmiBase() {
  SetupSmiKeys();
  map = new Map;
}


function MapSetupSmi() {
  MapSetupSmiBase();
  MapSetSmi();
}


function MapSetupStringBase() {
  SetupStringKeys();
  map = new Map;
}


function MapSetupString() {
  MapSetupStringBase();
  MapSetString();
}


function MapSetupObjectBase() {
  SetupObjectKeys();
64
  map = new Map;
65 66 67 68 69 70
}


function MapSetupObject() {
  MapSetupObjectBase();
  MapSetObject();
71 72 73 74 75 76 77 78
}


function MapTearDown() {
  map = null;
}


79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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
function MapSetSmi() {
  for (var i = 0; i < N; i++) {
    map.set(keys[i], i);
  }
}


function MapHasSmi() {
  for (var i = 0; i < N; i++) {
    if (!map.has(keys[i])) {
      throw new Error();
    }
  }
  for (var i = N; i < 2 * N; i++) {
    if (map.has(keys[i])) {
      throw new Error();
    }
  }
}


function MapGetSmi() {
  for (var i = 0; i < N; i++) {
    if (map.get(keys[i]) !== i) {
      throw new Error();
    }
  }
  for (var i = N; i < 2 * N; i++) {
    if (map.get(keys[i]) !== undefined) {
      throw new Error();
    }
  }
}


function MapDeleteSmi() {
  // This is run more than once per setup so we will end up deleting items
  // more than once. Therefore, we do not the return value of delete.
  for (var i = 0; i < N; i++) {
    map.delete(keys[i]);
  }
}


function MapSetString() {
  for (var i = 0; i < N; i++) {
    map.set(keys[i], i);
  }
}


function MapHasString() {
  for (var i = 0; i < N; i++) {
    if (!map.has(keys[i])) {
      throw new Error();
    }
  }
  for (var i = N; i < 2 * N; i++) {
    if (map.has(keys[i])) {
      throw new Error();
    }
  }
}


function MapGetString() {
  for (var i = 0; i < N; i++) {
    if (map.get(keys[i]) !== i) {
      throw new Error();
    }
  }
  for (var i = N; i < 2 * N; i++) {
    if (map.get(keys[i]) !== undefined) {
      throw new Error();
    }
  }
}


function MapDeleteString() {
  // This is run more than once per setup so we will end up deleting items
  // more than once. Therefore, we do not the return value of delete.
  for (var i = 0; i < N; i++) {
    map.delete(keys[i]);
  }
}


function MapSetObject() {
  for (var i = 0; i < N; i++) {
    map.set(keys[i], i);
  }
171 172 173
}


174
function MapHasObject() {
175
  for (var i = 0; i < N; i++) {
176
    if (!map.has(keys[i])) {
177 178 179 180
      throw new Error();
    }
  }
  for (var i = N; i < 2 * N; i++) {
181
    if (map.has(keys[i])) {
182 183 184 185 186 187
      throw new Error();
    }
  }
}


188
function MapGetObject() {
189
  for (var i = 0; i < N; i++) {
190
    if (map.get(keys[i]) !== i) {
191 192 193 194
      throw new Error();
    }
  }
  for (var i = N; i < 2 * N; i++) {
195
    if (map.get(keys[i]) !== undefined) {
196 197 198 199 200 201
      throw new Error();
    }
  }
}


202
function MapDeleteObject() {
203 204 205
  // This is run more than once per setup so we will end up deleting items
  // more than once. Therefore, we do not the return value of delete.
  for (var i = 0; i < N; i++) {
206
    map.delete(keys[i]);
207 208 209 210 211 212 213 214 215 216 217
  }
}


function MapForEach() {
  map.forEach(function(v, k) {
    if (v !== k) {
      throw new Error();
    }
  });
}