set.js 5.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
var SetSmiBenchmark = new BenchmarkSuite('Set-Smi', [1000], [
  new Benchmark('Set', false, false, 0, SetAddSmi, SetSetupSmiBase, SetTearDown),
  new Benchmark('Has', false, false, 0, SetHasSmi, SetSetupSmi, SetTearDown),
  new Benchmark('Delete', false, false, 0, SetDeleteSmi, SetSetupSmi, SetTearDown),
]);


var SetStringBenchmark = new BenchmarkSuite('Set-String', [1000], [
  new Benchmark('Set', false, false, 0, SetAddString, SetSetupStringBase, SetTearDown),
  new Benchmark('Has', false, false, 0, SetHasString, SetSetupString, SetTearDown),
  new Benchmark('Delete', false, false, 0, SetDeleteString, SetSetupString, SetTearDown),
]);


var SetObjectBenchmark = new BenchmarkSuite('Set-Object', [1000], [
  new Benchmark('Set', false, false, 0, SetAddObject, SetSetupObjectBase, SetTearDown),
  new Benchmark('Has', false, false, 0, SetHasObject, SetSetupObject, SetTearDown),
  new Benchmark('Delete', false, false, 0, SetDeleteObject, SetSetupObject, SetTearDown),
]);


27 28 29 30 31 32 33
var SetDoubleBenchmark = new BenchmarkSuite('Set-Double', [1000], [
  new Benchmark('Set', false, false, 0, SetAddDouble, SetSetupDoubleBase, SetTearDown),
  new Benchmark('Has', false, false, 0, SetHasDouble, SetSetupDouble, SetTearDown),
  new Benchmark('Delete', false, false, 0, SetDeleteDouble, SetSetupDouble, SetTearDown),
]);


34 35
var SetIterationBenchmark = new BenchmarkSuite('Set-Iteration', [1000], [
  new Benchmark('ForEach', false, false, 0, SetForEach, SetSetupSmi, SetTearDown),
36 37 38
]);


39 40 41 42 43
var SetIterationBenchmark = new BenchmarkSuite('Set-Iterator', [1000], [
  new Benchmark('Iterator', false, false, 0, SetIterator, SetSetupSmi, SetTearDown),
]);


44 45 46 47 48 49 50
var SetConstructorBenchmark = new BenchmarkSuite('Set-Constructor', [1000], [
  new Benchmark('Smi', false, false, 0, SetConstructorSmi, SetupSmiKeys, SetTearDown),
  new Benchmark('String', false, false, 0, SetConstructorString, SetupStringKeys, SetTearDown),
  new Benchmark('Object', false, false, 0, SetConstructorObject, SetupObjectKeys, SetTearDown),
  new Benchmark('Double', false, false, 0, SetConstructorDouble, SetupDoubleKeys, SetTearDown),
]);

51 52 53
var set;


54 55
function SetSetupSmiBase() {
  SetupSmiKeys();
56
  set = new Set;
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 84 85 86 87 88 89
}


function SetSetupSmi() {
  SetSetupSmiBase();
  SetAddSmi();
}


function SetSetupStringBase() {
  SetupStringKeys();
  set = new Set;
}


function SetSetupString() {
  SetSetupStringBase();
  SetAddString();
}


function SetSetupObjectBase() {
  SetupObjectKeys();
  set = new Set;
}


function SetSetupObject() {
  SetSetupObjectBase();
  SetAddObject();
}


90 91 92 93 94 95 96 97 98 99 100 101
function SetSetupDoubleBase() {
  SetupDoubleKeys();
  set = new Set;
}


function SetSetupDouble() {
  SetSetupDoubleBase();
  SetAddDouble();
}


102 103 104 105 106
function SetTearDown() {
  set = null;
}


107 108 109 110 111
function SetConstructorSmi() {
  set = new Set(keys);
}


112
function SetAddSmi() {
113
  for (var i = 0; i < N; i++) {
114
    set.add(keys[i], i);
115 116 117 118
  }
}


119 120 121 122 123 124 125 126 127 128 129
function SetHasSmi() {
  for (var i = 0; i < N; i++) {
    if (!set.has(keys[i])) {
      throw new Error();
    }
  }
  for (var i = N; i < 2 * N; i++) {
    if (set.has(keys[i])) {
      throw new Error();
    }
  }
130 131 132
}


133 134 135 136 137 138 139 140 141
function SetDeleteSmi() {
  // 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++) {
    set.delete(keys[i]);
  }
}


142 143 144 145 146
function SetConstructorString() {
  set = new Set(keys);
}


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 173 174 175 176
function SetAddString() {
  for (var i = 0; i < N; i++) {
    set.add(keys[i], i);
  }
}


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


function SetDeleteString() {
  // 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++) {
    set.delete(keys[i]);
  }
}


177 178 179 180 181
function SetConstructorObject() {
  set = new Set(keys);
}


182 183 184 185
function SetAddObject() {
  for (var i = 0; i < N; i++) {
    set.add(keys[i], i);
  }
186 187 188
}


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


203
function SetDeleteObject() {
204 205 206
  // 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++) {
207
    set.delete(keys[i]);
208 209 210 211
  }
}


212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
function SetConstructorDouble() {
  set = new Set(keys);
}


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


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


function SetDeleteDouble() {
  // 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++) {
    set.delete(keys[i]);
  }
}


247 248 249 250 251 252 253
function SetForEach() {
  set.forEach(function(v, k) {
    if (v !== k) {
      throw new Error();
    }
  });
}
254 255 256 257 258 259 260


function SetIterator() {
  var result = 0;
  for (const v of set) result += v;
  return result;
}