Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 SetBenchmark = new BenchmarkSuite('Set', [1000], [ |
| 7 | new Benchmark('Add', false, false, 0, SetAdd), |
| 8 | new Benchmark('Has', false, false, 0, SetHas, SetSetup, SetTearDown), |
| 9 | new Benchmark('Delete', false, false, 0, SetDelete, SetSetup, SetTearDown), |
| 10 | new Benchmark('ForEach', false, false, 0, SetForEach, SetSetup, SetTearDown), |
| 11 | ]); |
| 12 | |
| 13 | |
| 14 | var set; |
| 15 | var N = 10; |
| 16 | |
| 17 | |
| 18 | function SetSetup() { |
| 19 | set = new Set; |
| 20 | for (var i = 0; i < N; i++) { |
| 21 | set.add(i); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | |
| 26 | function SetTearDown() { |
| 27 | map = null; |
| 28 | } |
| 29 | |
| 30 | |
| 31 | function SetAdd() { |
| 32 | SetSetup(); |
| 33 | SetTearDown(); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | function SetHas() { |
| 38 | for (var i = 0; i < N; i++) { |
| 39 | if (!set.has(i)) { |
| 40 | throw new Error(); |
| 41 | } |
| 42 | } |
| 43 | for (var i = N; i < 2 * N; i++) { |
| 44 | if (set.has(i)) { |
| 45 | throw new Error(); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | |
| 51 | function SetDelete() { |
| 52 | // This is run more than once per setup so we will end up deleting items |
| 53 | // more than once. Therefore, we do not the return value of delete. |
| 54 | for (var i = 0; i < N; i++) { |
| 55 | set.delete(i); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | function SetForEach() { |
| 61 | set.forEach(function(v, k) { |
| 62 | if (v !== k) { |
| 63 | throw new Error(); |
| 64 | } |
| 65 | }); |
| 66 | } |