Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 1 | // 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 | |
| 8 | var 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. |
| 11 | for (var i = 0; i < 1e5; i++) { |
| 12 | largeArray[i] += 6.66; |
| 13 | } |
| 14 | |
| 15 | // ---------------------------------------------------------------------------- |
| 16 | // Benchmark: Spread |
| 17 | // ---------------------------------------------------------------------------- |
| 18 | |
| 19 | function 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 | |
| 30 | function 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 | |
| 44 | function 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 | |
| 58 | function 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 | |
| 69 | function 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 | |
| 80 | function 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 | |
| 91 | function 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 | |
| 102 | function 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 | |
| 116 | function 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 | |
| 127 | load('../base.js'); |
| 128 | |
| 129 | var success = true; |
| 130 | |
| 131 | function PrintResult(name, result) { |
| 132 | print(name + '-ArrayLiteralInitialSpreadLargeDoublePacked(Score): ' + result); |
| 133 | } |
| 134 | |
| 135 | function 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. |
| 141 | function CreateBenchmark(name, f) { |
| 142 | new BenchmarkSuite(name, [1000], [ new Benchmark(name, false, false, 5, f) ]); |
| 143 | } |
| 144 | |
| 145 | CreateBenchmark('Spread', SpreadLarge); |
| 146 | CreateBenchmark('ForLength', ForLengthLarge); |
| 147 | CreateBenchmark('ForLengthEmpty', ForLengthEmptyLarge); |
| 148 | CreateBenchmark('Slice', SliceLarge); |
| 149 | CreateBenchmark('Slice0', Slice0Large); |
| 150 | CreateBenchmark('ConcatReceive', ConcatReceiveLarge); |
| 151 | CreateBenchmark('ConcatArg', ConcatArgLarge); |
| 152 | |
| 153 | BenchmarkSuite.config.doWarmup = true; |
| 154 | BenchmarkSuite.config.doDeterministic = true; |
| 155 | BenchmarkSuite.RunSuites({NotifyResult: PrintResult, NotifyError: PrintError}); |