weakset.js 1.46 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.


var SetBenchmark = new BenchmarkSuite('WeakSet', [1000], [
7 8
  new Benchmark('Add', false, false, 0, WeakSetAdd, WeakSetSetupBase,
      WeakSetTearDown),
9 10 11 12 13 14
  new Benchmark('Has', false, false, 0, WeakSetHas, WeakSetSetup,
      WeakSetTearDown),
  new Benchmark('Delete', false, false, 0, WeakSetDelete, WeakSetSetup,
      WeakSetTearDown),
]);

15 16 17 18
var SetBenchmark = new BenchmarkSuite('WeakSet-Constructor', [1000], [
  new Benchmark('Constructor', false, false, 0, WeakSetConstructor, SetupObjectKeys,
      WeakSetTearDown),
]);
19 20 21 22

var ws;


23 24 25
function WeakSetSetupBase() {
  SetupObjectKeys();
  ws = new WeakSet;
26 27 28 29
}


function WeakSetSetup() {
30 31
  WeakSetSetupBase();
  WeakSetAdd();
32 33 34 35 36 37 38 39
}


function WeakSetTearDown() {
  ws = null;
}


40 41 42 43 44
function WeakSetConstructor() {
  ws = new WeakSet(keys);
}


45
function WeakSetAdd() {
46 47 48
  for (var i = 0; i < N; i++) {
    ws.add(keys[i]);
  }
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
}


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


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