blob: b310a719025ee98c201148f51100a177b8a5ca17 [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('Map', [1000], [
7 new Benchmark('Set', false, false, 0, MapSet),
8 new Benchmark('Has', false, false, 0, MapHas, MapSetup, MapTearDown),
9 new Benchmark('Get', false, false, 0, MapGet, MapSetup, MapTearDown),
10 new Benchmark('Delete', false, false, 0, MapDelete, MapSetup, MapTearDown),
11 new Benchmark('ForEach', false, false, 0, MapForEach, MapSetup, MapTearDown),
12]);
13
14
15var map;
16var N = 10;
17
18
19function MapSetup() {
20 map = new Map;
21 for (var i = 0; i < N; i++) {
22 map.set(i, i);
23 }
24}
25
26
27function MapTearDown() {
28 map = null;
29}
30
31
32function MapSet() {
33 MapSetup();
34 MapTearDown();
35}
36
37
38function MapHas() {
39 for (var i = 0; i < N; i++) {
40 if (!map.has(i)) {
41 throw new Error();
42 }
43 }
44 for (var i = N; i < 2 * N; i++) {
45 if (map.has(i)) {
46 throw new Error();
47 }
48 }
49}
50
51
52function MapGet() {
53 for (var i = 0; i < N; i++) {
54 if (map.get(i) !== i) {
55 throw new Error();
56 }
57 }
58 for (var i = N; i < 2 * N; i++) {
59 if (map.get(i) !== undefined) {
60 throw new Error();
61 }
62 }
63}
64
65
66function MapDelete() {
67 // This is run more than once per setup so we will end up deleting items
68 // more than once. Therefore, we do not the return value of delete.
69 for (var i = 0; i < N; i++) {
70 map.delete(i);
71 }
72}
73
74
75function MapForEach() {
76 map.forEach(function(v, k) {
77 if (v !== k) {
78 throw new Error();
79 }
80 });
81}