2017-12-05 12:23:48 +00:00
|
|
|
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2017-12-06 13:55:06 +00:00
|
|
|
(() => {
|
2017-12-05 12:23:48 +00:00
|
|
|
|
|
|
|
function benchy(name, test, testSetup) {
|
|
|
|
new BenchmarkSuite(name, [1000],
|
|
|
|
[
|
|
|
|
new Benchmark(name, false, false, 0, test, testSetup, ()=>{})
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-06 13:55:06 +00:00
|
|
|
benchy('NaiveFindReplacement', Naive, NaiveSetup);
|
|
|
|
benchy('DoubleFind', Double, DoubleSetup);
|
|
|
|
benchy('SmiFind', Smi, SmiSetup);
|
|
|
|
benchy('FastFind', Fast, FastSetup);
|
|
|
|
benchy('GenericFind', Generic, ObjectSetup);
|
|
|
|
benchy('OptFastFind', OptFast, FastSetup);
|
2017-12-05 12:23:48 +00:00
|
|
|
|
2017-12-06 13:55:06 +00:00
|
|
|
let array;
|
2017-12-05 12:23:48 +00:00
|
|
|
// Initialize func variable to ensure the first test doesn't benefit from
|
|
|
|
// global object property tracking.
|
2017-12-06 13:55:06 +00:00
|
|
|
let func = 0;
|
|
|
|
let result;
|
|
|
|
const array_size = 100;
|
|
|
|
const max_index = array_size - 1;
|
2017-12-05 12:23:48 +00:00
|
|
|
|
|
|
|
// Although these functions have the same code, they are separated for
|
|
|
|
// clean IC feedback.
|
2017-12-06 13:55:06 +00:00
|
|
|
function Double() {
|
|
|
|
result = array.find(func);
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
2017-12-06 13:55:06 +00:00
|
|
|
function Smi() {
|
|
|
|
result = array.find(func);
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
2017-12-06 13:55:06 +00:00
|
|
|
function Fast() {
|
|
|
|
result = array.find(func);
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we inline the callback, pick up all possible TurboFan
|
|
|
|
// optimizations.
|
2017-12-06 13:55:06 +00:00
|
|
|
function RunOptFast(multiple) {
|
2017-12-05 12:23:48 +00:00
|
|
|
// Use of variable multiple in the callback function forces
|
|
|
|
// context creation without escape analysis.
|
|
|
|
//
|
|
|
|
// Also, the arrow function requires inlining based on
|
|
|
|
// SharedFunctionInfo.
|
2017-12-06 13:55:06 +00:00
|
|
|
result = array.find((v, i, a) => v === `value ${multiple}`);
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 13:55:06 +00:00
|
|
|
// Don't optimize because I want to optimize RunOptFast with a parameter
|
2017-12-05 12:23:48 +00:00
|
|
|
// to be used in the callback.
|
2017-12-06 13:55:06 +00:00
|
|
|
%NeverOptimizeFunction(OptFast);
|
|
|
|
function OptFast() { RunOptFast(max_index); }
|
2017-12-05 12:23:48 +00:00
|
|
|
|
2017-12-06 13:55:06 +00:00
|
|
|
function Naive() {
|
2017-12-05 12:23:48 +00:00
|
|
|
let index = -1;
|
2017-12-06 13:55:06 +00:00
|
|
|
const length = array == null ? 0 : array.length;
|
2017-12-05 12:23:48 +00:00
|
|
|
|
|
|
|
for (let index = 0; index < length; index++) {
|
2017-12-06 13:55:06 +00:00
|
|
|
const value = array[index];
|
|
|
|
if (func(value, index, array)) {
|
|
|
|
result = value;
|
2017-12-05 12:23:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 13:55:06 +00:00
|
|
|
function Generic() {
|
|
|
|
result = Array.prototype.find.call(array, func);
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 13:55:06 +00:00
|
|
|
function NaiveSetup() {
|
|
|
|
// Prime Naive with polymorphic cases.
|
|
|
|
array = [1, 2, 3];
|
|
|
|
func = (v, i, a) => v === max_index;
|
|
|
|
Naive();
|
|
|
|
Naive();
|
|
|
|
array = [3.4]; Naive();
|
|
|
|
array = new Array(10); array[0] = 'hello'; Naive();
|
|
|
|
SmiSetup();
|
|
|
|
delete array[1];
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 13:55:06 +00:00
|
|
|
function SmiSetup() {
|
|
|
|
array = Array.from({ length: array_size }, (_, i) => i);
|
|
|
|
func = (value, index, object) => value === max_index;
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 13:55:06 +00:00
|
|
|
function DoubleSetup() {
|
|
|
|
array = Array.from({ length: array_size }, (_, i) => i + 0.5);
|
|
|
|
func = (value, index, object) => value === max_index + 0.5;
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 13:55:06 +00:00
|
|
|
function FastSetup() {
|
|
|
|
array = Array.from({ length: array_size }, (_, i) => `value ${i}`);
|
|
|
|
func = (value, index, object) => value === `value ${max_index}`;
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 13:55:06 +00:00
|
|
|
function ObjectSetup() {
|
|
|
|
array = { length: array_size };
|
|
|
|
for (let i = 0; i < array_size; i++) {
|
|
|
|
array[i] = i;
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
2017-12-06 13:55:06 +00:00
|
|
|
func = (value, index, object) => value === max_index;
|
2017-12-05 12:23:48 +00:00
|
|
|
}
|
2017-12-06 13:55:06 +00:00
|
|
|
|
|
|
|
})();
|