OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 function benchy(name, test, testSetup) { |
| 6 new BenchmarkSuite(name, [1000], |
| 7 [ |
| 8 new Benchmark(name, false, false, 0, test, testSetup, ()=>{}) |
| 9 ]); |
| 10 } |
| 11 |
| 12 benchy('NaiveMapReplacement', NaiveMap, NaiveMapSetup); |
| 13 benchy('DoubleMap', DoubleMap, DoubleMapSetup); |
| 14 benchy('SmiMap', SmiMap, SmiMapSetup); |
| 15 benchy('FastMap', FastMap, FastMapSetup); |
| 16 benchy('ObjectMap', GenericMap, ObjectMapSetup); |
| 17 |
| 18 var array; |
| 19 var func; |
| 20 var this_arg; |
| 21 var result; |
| 22 var array_size = 100; |
| 23 |
| 24 // Although these functions have the same code, they are separated for |
| 25 // clean IC feedback. |
| 26 function DoubleMap() { |
| 27 result = array.map(func, this_arg); |
| 28 } |
| 29 function SmiMap() { |
| 30 result = array.map(func, this_arg); |
| 31 } |
| 32 function FastMap() { |
| 33 result = array.map(func, this_arg); |
| 34 } |
| 35 |
| 36 function NaiveMap() { |
| 37 let index = -1 |
| 38 const length = array == null ? 0 : array.length |
| 39 const result = new Array(length) |
| 40 |
| 41 while (++index < length) { |
| 42 result[index] = func(array[index], index, array) |
| 43 } |
| 44 return result |
| 45 } |
| 46 |
| 47 |
| 48 function GenericMap() { |
| 49 result = Array.prototype.map.call(array, func, this_arg); |
| 50 } |
| 51 |
| 52 function NaiveMapSetup() { |
| 53 // Prime NaiveMap with polymorphic cases. |
| 54 array = [1, 2, 3]; |
| 55 func = (v, i, a) => v; |
| 56 NaiveMap(); |
| 57 NaiveMap(); |
| 58 array = [3.4]; NaiveMap(); |
| 59 array = new Array(10); array[0] = 'hello'; NaiveMap(); |
| 60 SmiMapSetup(); |
| 61 delete array[1]; |
| 62 } |
| 63 |
| 64 function SmiMapSetup() { |
| 65 array = new Array(); |
| 66 for (var i = 0; i < array_size; i++) array[i] = i; |
| 67 func = (value, index, object) => { return value; }; |
| 68 } |
| 69 |
| 70 function DoubleMapSetup() { |
| 71 array = new Array(); |
| 72 for (var i = 0; i < array_size; i++) array[i] = (i + 0.5); |
| 73 func = (value, index, object) => { return value; }; |
| 74 } |
| 75 |
| 76 function FastMapSetup() { |
| 77 array = new Array(); |
| 78 for (var i = 0; i < array_size; i++) array[i] = 'value ' + i; |
| 79 func = (value, index, object) => { return value; }; |
| 80 } |
| 81 |
| 82 function ObjectMapSetup() { |
| 83 array = { length: array_size }; |
| 84 for (var i = 0; i < array_size; i++) { |
| 85 array[i] = i; |
| 86 } |
| 87 func = (value, index, object) => { return value; }; |
| 88 } |
OLD | NEW |