blob: 8736dfd58b7f26c54a169ce3332e5caf424feec0 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5
6var MapBenchmark = new BenchmarkSuite('WeakMap', [1000], [
7 new Benchmark('Set', false, false, 0, WeakMapSet),
8 new Benchmark('Has', false, false, 0, WeakMapHas, WeakMapSetup,
9 WeakMapTearDown),
10 new Benchmark('Get', false, false, 0, WeakMapGet, WeakMapSetup,
11 WeakMapTearDown),
12 new Benchmark('Delete', false, false, 0, WeakMapDelete, WeakMapSetup,
13 WeakMapTearDown),
14]);
15
16
17var wm;
18var N = 10;
19var keys = [];
20
21
22for (var i = 0; i < N * 2; i++) {
23 keys[i] = {};
24}
25
26
27function WeakMapSetup() {
28 wm = new WeakMap;
29 for (var i = 0; i < N; i++) {
30 wm.set(keys[i], i);
31 }
32}
33
34
35function WeakMapTearDown() {
36 wm = null;
37}
38
39
40function WeakMapSet() {
41 WeakMapSetup();
42 WeakMapTearDown();
43}
44
45
46function WeakMapHas() {
47 for (var i = 0; i < N; i++) {
48 if (!wm.has(keys[i])) {
49 throw new Error();
50 }
51 }
52 for (var i = N; i < 2 * N; i++) {
53 if (wm.has(keys[i])) {
54 throw new Error();
55 }
56 }
57}
58
59
60function WeakMapGet() {
61 for (var i = 0; i < N; i++) {
62 if (wm.get(keys[i]) !== i) {
63 throw new Error();
64 }
65 }
66 for (var i = N; i < 2 * N; i++) {
67 if (wm.get(keys[i]) !== undefined) {
68 throw new Error();
69 }
70 }
71}
72
73
74function WeakMapDelete() {
75 // This is run more than once per setup so we will end up deleting items
76 // more than once. Therefore, we do not the return value of delete.
77 for (var i = 0; i < N; i++) {
78 wm.delete(keys[i]);
79 }
80}