Home | History | Annotate | Download | only in Collections
      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('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 
     15 var map;
     16 var N = 10;
     17 
     18 
     19 function MapSetup() {
     20   map = new Map;
     21   for (var i = 0; i < N; i++) {
     22     map.set(i, i);
     23   }
     24 }
     25 
     26 
     27 function MapTearDown() {
     28   map = null;
     29 }
     30 
     31 
     32 function MapSet() {
     33   MapSetup();
     34   MapTearDown();
     35 }
     36 
     37 
     38 function 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 
     52 function 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 
     66 function 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 
     75 function MapForEach() {
     76   map.forEach(function(v, k) {
     77     if (v !== k) {
     78       throw new Error();
     79     }
     80   });
     81 }
     82