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