blob: 2654d53d9c4c2f12d0574a01c4fd217a25eb3e86 [file] [log] [blame]
Andrew Top63c7ad42019-11-25 16:10:13 -08001// Copyright 2018 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// Comparing different iterating schemes against spread initial literals.
6// Benchmarks for large smi maps.
7
8var keys = Array.from(Array(1e4).keys());
9var keyValuePairs = keys.map((value) => [value, value + 1]);
10var map = new Map(keyValuePairs);
11
12// ----------------------------------------------------------------------------
13// Benchmark: SpreadKeys
14// ----------------------------------------------------------------------------
15
16function SpreadKeys() {
17 var newArr = [...map.keys()];
18 // basic sanity check
19 if (newArr.length != map.size) throw 666;
20 return newArr;
21}
22
23// ----------------------------------------------------------------------------
24// Benchmark: SpreadValues
25// ----------------------------------------------------------------------------
26
27function SpreadValues() {
28 var newArr = [...map.values()];
29 // basic sanity check
30 if (newArr.length != map.size) throw 666;
31 return newArr;
32}
33
34// ----------------------------------------------------------------------------
35// Benchmark: ForOfKeys
36// ----------------------------------------------------------------------------
37
38function ForOfKeys() {
39 var newArr = new Array(map.size);
40 var i = 0;
41 for (let x of map.keys()) {
42 newArr[i] = x;
43 i++;
44 }
45 if (newArr.length != map.size) throw 666;
46 return newArr;
47}
48
49// ----------------------------------------------------------------------------
50// Benchmark: ForOfValues
51// ----------------------------------------------------------------------------
52
53function ForOfValues() {
54 var newArr = new Array(map.size);
55 var i = 0;
56 for (let x of map.values()) {
57 newArr[i] = x;
58 i++;
59 }
60 if (newArr.length != map.size) throw 666;
61 return newArr;
62}
63
64
65// ----------------------------------------------------------------------------
66// Setup and Run
67// ----------------------------------------------------------------------------
68
69load('../base.js');
70
71var success = true;
72
73function PrintResult(name, result) {
74 print(name + '-ArrayLiteralInitialSpreadLargeSmiMap(Score): ' + result);
75}
76
77function PrintError(name, error) {
78 PrintResult('Error: ' + name, error);
79 success = false;
80}
81
82function CreateBenchmark(name, f) {
83 new BenchmarkSuite(name, [1000], [ new Benchmark(name, false, false, 0, f) ]);
84}
85
86CreateBenchmark('ForOfKeys', ForOfKeys);
87CreateBenchmark('ForOfValues', ForOfValues);
88CreateBenchmark('SpreadKeys', SpreadKeys);
89CreateBenchmark('SpreadValues', SpreadValues);
90
91
92BenchmarkSuite.config.doWarmup = true;
93BenchmarkSuite.config.doDeterministic = false;
94BenchmarkSuite.RunSuites({NotifyResult: PrintResult, NotifyError: PrintError});