OLD | NEW |
| (Empty) |
1 // 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 | |
6 var 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 | |
17 var wm; | |
18 var N = 10; | |
19 var keys = []; | |
20 | |
21 | |
22 for (var i = 0; i < N * 2; i++) { | |
23 keys[i] = {}; | |
24 } | |
25 | |
26 | |
27 function WeakMapSetup() { | |
28 wm = new WeakMap; | |
29 for (var i = 0; i < N; i++) { | |
30 wm.set(keys[i], i); | |
31 } | |
32 } | |
33 | |
34 | |
35 function WeakMapTearDown() { | |
36 wm = null; | |
37 } | |
38 | |
39 | |
40 function WeakMapSet() { | |
41 WeakMapSetup(); | |
42 WeakMapTearDown(); | |
43 } | |
44 | |
45 | |
46 function 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 | |
60 function 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 | |
74 function 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 } | |
OLD | NEW |