blob: 9b449840ea7ab046db87a7b60ac1ce7919b9d2ca [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 copy schemes against spread initial literals.
6// Benchmarks for large packed double arrays.
7
8var largeArray = Array.from(Array(1e5).keys());
9// TODO(dhai): we should be able to use Array.prototype.map here, but that
10// implementation is currently creating a HOLEY array.
11for (var i = 0; i < 1e5; i++) {
12 largeArray[i] += 6.66;
13}
14
15// ----------------------------------------------------------------------------
16// Benchmark: Spread
17// ----------------------------------------------------------------------------
18
19function SpreadLarge() {
20 var newArr = [...largeArray];
21 // basic sanity check
22 if (newArr.length != largeArray.length) throw 666;
23 return newArr;
24}
25
26// ----------------------------------------------------------------------------
27// Benchmark: ForLength
28// ----------------------------------------------------------------------------
29
30function ForLengthLarge() {
31 var newArr = new Array(largeArray.length);
32 for (let i = 0; i < largeArray.length; i++) {
33 newArr[i] = largeArray[i];
34 }
35 // basic sanity check
36 if (newArr.length != largeArray.length) throw 666;
37 return newArr;
38}
39
40// ----------------------------------------------------------------------------
41// Benchmark: ForLengthEmpty
42// ----------------------------------------------------------------------------
43
44function ForLengthEmptyLarge() {
45 var newArr = [];
46 for (let i = 0; i < largeArray.length; i++) {
47 newArr[i] = largeArray[i];
48 }
49 // basic sanity check
50 if (newArr.length != largeArray.length) throw 666;
51 return newArr;
52}
53
54// ----------------------------------------------------------------------------
55// Benchmark: Slice
56// ----------------------------------------------------------------------------
57
58function SliceLarge() {
59 var newArr = largeArray.slice();
60 // basic sanity check
61 if (newArr.length != largeArray.length) throw 666;
62 return newArr;
63}
64
65// ----------------------------------------------------------------------------
66// Benchmark: Slice0
67// ----------------------------------------------------------------------------
68
69function Slice0Large() {
70 var newArr = largeArray.slice(0);
71 // basic sanity check
72 if (newArr.length != largeArray.length) throw 666;
73 return newArr;
74}
75
76// ----------------------------------------------------------------------------
77// Benchmark: ConcatReceive
78// ----------------------------------------------------------------------------
79
80function ConcatReceiveLarge() {
81 var newArr = largeArray.concat();
82 // basic sanity check
83 if (newArr.length != largeArray.length) throw 666;
84 return newArr;
85}
86
87// ----------------------------------------------------------------------------
88// Benchmark: ConcatArg
89// ----------------------------------------------------------------------------
90
91function ConcatArgLarge() {
92 var newArr = [].concat(largeArray);
93 // basic sanity check
94 if (newArr.length != largeArray.length) throw 666;
95 return newArr;
96}
97
98// ----------------------------------------------------------------------------
99// Benchmark: ForOfPush
100// ----------------------------------------------------------------------------
101
102function ForOfPushLarge() {
103 var newArr = [];
104 for (let x of largeArray) {
105 newArr.push(x)
106 }
107 // basic sanity check
108 if (newArr.length != largeArray.length) throw 666;
109 return newArr;
110}
111
112// ----------------------------------------------------------------------------
113// Benchmark: MapId
114// ----------------------------------------------------------------------------
115
116function MapIdLarge() {
117 var newArr = largeArray.map(x => x);
118 // basic sanity check
119 if (newArr.length != largeArray.length) throw 666;
120 return newArr;
121}
122
123// ----------------------------------------------------------------------------
124// Setup and Run
125// ----------------------------------------------------------------------------
126
127load('../base.js');
128
129var success = true;
130
131function PrintResult(name, result) {
132 print(name + '-ArrayLiteralInitialSpreadLargeDoublePacked(Score): ' + result);
133}
134
135function PrintError(name, error) {
136 PrintResult('Error: ' + name, error);
137 success = false;
138}
139
140// Run the benchmark (5 x 100) iterations instead of 1 second.
141function CreateBenchmark(name, f) {
142 new BenchmarkSuite(name, [1000], [ new Benchmark(name, false, false, 5, f) ]);
143}
144
145CreateBenchmark('Spread', SpreadLarge);
146CreateBenchmark('ForLength', ForLengthLarge);
147CreateBenchmark('ForLengthEmpty', ForLengthEmptyLarge);
148CreateBenchmark('Slice', SliceLarge);
149CreateBenchmark('Slice0', Slice0Large);
150CreateBenchmark('ConcatReceive', ConcatReceiveLarge);
151CreateBenchmark('ConcatArg', ConcatArgLarge);
152
153BenchmarkSuite.config.doWarmup = true;
154BenchmarkSuite.config.doDeterministic = true;
155BenchmarkSuite.RunSuites({NotifyResult: PrintResult, NotifyError: PrintError});