OLD | NEW |
1 // Copyright 2017 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 new BenchmarkSuite('Filter', [1000], [ | 5 function benchy(name, test, testSetup) { |
6 new Benchmark('SmiFilter', false, false, 0, | 6 new BenchmarkSuite(name, [1000], |
7 Filter, SmiFilterSetup, ()=>{}), | 7 [ |
8 new Benchmark('DoubleFilter', false, false, 0, | 8 new Benchmark(name, false, false, 0, test, testSetup, ()=>{}) |
9 Filter, DoubleFilterSetup, ()=>{}), | 9 ]); |
10 new Benchmark('FastFilter', false, false, 0, | 10 } |
11 Filter, FastFilterSetup, ()=>{}), | 11 |
12 new Benchmark('HoleySmiFilter', false, false, 0, | 12 benchy('NaiveFilterReplacement', NaiveFilter, NaiveFilterSetup); |
13 Filter, HoleySmiFilterSetup, ()=>{}), | 13 benchy('DoubleFilter', DoubleFilter, DoubleFilterSetup); |
14 new Benchmark('HoleyDoubleFilter', false, false, 0, | 14 benchy('SmiFilter', SmiFilter, SmiFilterSetup); |
15 Filter, HoleyDoubleFilterSetup, ()=>{}), | 15 benchy('FastFilter', FastFilter, FastFilterSetup); |
16 new Benchmark('HoleyFastFilter', false, false, 0, | 16 benchy('ObjectFilter', GenericFilter, ObjectFilterSetup); |
17 Filter, HoleyFastFilterSetup, ()=>{}), | |
18 new Benchmark('ObjectFilter', false, false, 0, | |
19 GenericFilter, ObjectFilterSetup, ()=>{}), | |
20 ]); | |
21 | 17 |
22 var array; | 18 var array; |
23 var func; | 19 var func; |
24 var this_arg; | 20 var this_arg; |
25 var result; | 21 var result; |
26 var array_size = 100; | 22 var array_size = 100; |
27 | 23 |
28 function Filter() { | 24 // Although these functions have the same code, they are separated for |
| 25 // clean IC feedback. |
| 26 function DoubleFilter() { |
| 27 result = array.filter(func, this_arg); |
| 28 } |
| 29 function SmiFilter() { |
| 30 result = array.filter(func, this_arg); |
| 31 } |
| 32 function FastFilter() { |
29 result = array.filter(func, this_arg); | 33 result = array.filter(func, this_arg); |
30 } | 34 } |
31 | 35 |
32 function GenericFilter() { | 36 function GenericFilter() { |
33 result = Array.prototype.filter.call(array, func, this_arg); | 37 result = Array.prototype.filter.call(array, func, this_arg); |
34 } | 38 } |
35 | 39 |
| 40 // From the lodash implementation. |
| 41 function NaiveFilter() { |
| 42 let index = -1 |
| 43 let resIndex = 0 |
| 44 const length = array == null ? 0 : array.length |
| 45 const result = [] |
| 46 |
| 47 while (++index < length) { |
| 48 const value = array[index] |
| 49 if (func(value, index, array)) { |
| 50 result[resIndex++] = value |
| 51 } |
| 52 } |
| 53 return result |
| 54 } |
| 55 |
| 56 function NaiveFilterSetup() { |
| 57 // Prime NaiveFilter with polymorphic cases. |
| 58 array = [1, 2, 3]; |
| 59 func = ()=>true; |
| 60 NaiveFilter(); |
| 61 NaiveFilter(); |
| 62 array = [3.4]; NaiveFilter(); |
| 63 array = new Array(10); array[0] = 'hello'; NaiveFilter(); |
| 64 SmiFilterSetup(); |
| 65 delete array[1]; |
| 66 } |
| 67 |
36 function SmiFilterSetup() { | 68 function SmiFilterSetup() { |
37 array = new Array(); | 69 array = new Array(); |
38 for (var i = 0; i < array_size; i++) array[i] = i; | 70 for (var i = 0; i < array_size; i++) array[i] = i; |
39 func = (value, index, object) => { return value % 2 === 0; }; | 71 func = (value, index, object) => { return value % 2 === 0; }; |
40 } | 72 } |
41 | 73 |
42 function HoleySmiFilterSetup() { | |
43 array = new Array(array_size); | |
44 for (var i = 0; i < array_size; i++) { | |
45 if (i % 2 === 0) array[i] = i; | |
46 } | |
47 func = (value, index, object) => { return value % 2 === 0; }; | |
48 } | |
49 | |
50 function DoubleFilterSetup() { | 74 function DoubleFilterSetup() { |
51 array = new Array(); | 75 array = new Array(); |
52 for (var i = 0; i < array_size; i++) array[i] = (i + 0.5); | 76 for (var i = 0; i < array_size; i++) array[i] = (i + 0.5); |
53 func = (value, index, object) => { return Math.floor(value) % 2 === 0; }; | 77 func = (value, index, object) => { return Math.floor(value) % 2 === 0; }; |
54 } | 78 } |
55 | 79 |
56 function HoleyDoubleFilterSetup() { | |
57 array = new Array(array_size); | |
58 for (var i = 0; i < array_size; i++) { | |
59 if (i != 3) { | |
60 array[i] = (i + 0.5); | |
61 } | |
62 } | |
63 func = (value, index, object) => { return Math.floor(value) % 2 === 0; }; | |
64 } | |
65 | |
66 function FastFilterSetup() { | 80 function FastFilterSetup() { |
67 array = new Array(); | 81 array = new Array(); |
68 for (var i = 0; i < array_size; i++) array[i] = 'value ' + i; | 82 for (var i = 0; i < array_size; i++) array[i] = 'value ' + i; |
69 func = (value, index, object) => { return index % 2 === 0; }; | 83 func = (value, index, object) => { return index % 2 === 0; }; |
70 } | 84 } |
71 | 85 |
72 function HoleyFastFilterSetup() { | |
73 array = new Array(array_size); | |
74 for (var i = 0; i < array_size; i++) { | |
75 if (i % 2 != 0) { | |
76 array[i] = 'value ' + i; | |
77 } | |
78 } | |
79 func = (value, index, object) => { return index % 2 === 0; }; | |
80 } | |
81 | |
82 function ObjectFilterSetup() { | 86 function ObjectFilterSetup() { |
83 array = { length: array_size }; | 87 array = { length: array_size }; |
84 for (var i = 0; i < array_size; i++) { | 88 for (var i = 0; i < array_size; i++) { |
85 array[i] = i; | 89 array[i] = i; |
86 } | 90 } |
87 func = (value, index, object) => { return index % 2 === 0; }; | 91 func = (value, index, object) => { return index % 2 === 0; }; |
88 } | 92 } |
OLD | NEW |