weakmap.js 2.82 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 MapBenchmark = new BenchmarkSuite('WeakMap', [1000], [
7 8
  new Benchmark('Set', false, false, 0, WeakMapSet, WeakMapSetupBase,
      WeakMapTearDown),
9 10 11 12 13 14 15 16
  new Benchmark('Has', false, false, 0, WeakMapHas, WeakMapSetup,
      WeakMapTearDown),
  new Benchmark('Get', false, false, 0, WeakMapGet, WeakMapSetup,
      WeakMapTearDown),
  new Benchmark('Delete', false, false, 0, WeakMapDelete, WeakMapSetup,
      WeakMapTearDown),
]);

17
var MapBenchmark = new BenchmarkSuite('WeakMapSetGet-Large', [1e7], [
18 19 20 21 22 23
  new Benchmark('Set-Get', false, true, 1, WeakMapSetGetLarge,
                WeakMapSetupBaseLarge, WeakMapTearDown),
]);

var MapBenchmark = new BenchmarkSuite('WeakMapSet-Huge', [1e8], [
  new Benchmark('Set-Get', false, true, 1, WeakMapSetHuge,
24 25 26
                WeakMapSetupBaseLarge, WeakMapTearDown),
]);

27 28 29 30 31
var MapBenchmark = new BenchmarkSuite('WeakMap-Constructor', [1000], [
  new Benchmark('Constructor', false, false, 0, WeakMapConstructor, SetupObjectKeyValuePairs,
      WeakMapTearDown),
]);

32 33 34 35

var wm;


36 37 38
function WeakMapSetupBase() {
  SetupObjectKeys();
  wm = new WeakMap;
39 40 41
}


42 43 44 45 46 47
function WeakMapSetupBaseLarge() {
  SetupObjectKeys(2 * LargeN);
  wm = new WeakMap;
}


48
function WeakMapSetup() {
49 50
  WeakMapSetupBase();
  WeakMapSet();
51 52 53 54 55 56 57 58
}


function WeakMapTearDown() {
  wm = null;
}


59 60 61 62
function WeakMapConstructor() {
  wm = new WeakMap(keyValuePairs);
}

63
function WeakMapSet() {
64 65 66
  for (var i = 0; i < N; i++) {
    wm.set(keys[i], i);
  }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
}


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


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


function WeakMapDelete() {
  // This is run more than once per setup so we will end up deleting items
100
  // more than once. Therefore, we do not check the return value of delete.
101 102 103 104
  for (var i = 0; i < N; i++) {
    wm.delete(keys[i]);
  }
}
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

function WeakMapSetGetLarge() {
  for (var i = 0; i < LargeN; i++) {
    wm.set(keys[i * 2], i);
  }
  for (var i = 0; i < LargeN; i++) {
    if (wm.get(keys[i * 2]) !== i) {
      throw new Error();
    }
  }
  for (var i = N; i < 2 * LargeN; i++) {
    if (wm.get(keys[i * 2 + 1]) !== undefined) {
      throw new Error();
    }
  }
}
121 122 123 124 125 126 127 128 129 130 131

function WeakMapSetHuge() {
  function Foo(i) {
    this.x = i;
  }
  let obj;
  for (let i = 0; i < LargeN; i++) {
    obj = new Foo(i);         // Make sure we do not scalar-replace the object.
    wm.set(obj, 1);
  }
}