Commit 2c7c8da1 authored by Mathias Bynens's avatar Mathias Bynens Committed by Commit Bot

[js-perf-test] Add String#{starts,ends}With micro-benchmark

This patch adds a micro-benchmark comparing
`string.startsWith(singleCodeUnit)`, `string[0]`,
`string.endsWith(singleCodeUnit)`, and `string[string.length - 1]`.

The benchmark can be used to measure any String#{starts,ends}With
optimizations we implement in the future.

Test:

    tools/run_perf.py --binary-override-path=out/x64.release/d8 \
      --filter=JSTests/Strings/StringStartsEndsWithComparison \
      --extra-flags=--trace-turbo test/js-perf-test/JSTests.json

Bug: v8:7453
Change-Id: I68cad197fbcbfc6b1938fc437776c319ee9f81df
Reviewed-on: https://chromium-review.googlesource.com/1011619Reviewed-by: 's avatarBenedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Mathias Bynens <mathias@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52589}
parent b8defbc9
......@@ -292,6 +292,20 @@
{"name": "StringMatchAllBuiltinZeroWidthUnicode"}
]
},
{
"name": "StringStartsEndsWithComparison",
"main": "run.js",
"resources": [ "string-startsendswith-comp.js" ],
"test_flags": [ "string-startsendswith-comp" ],
"results_regexp": "^%s\\-Strings\\(Score\\): (.+)$",
"run_count": 1,
"tests": [
{"name": "startsWith"},
{"name": "startsIndex"},
{"name": "endsWith"},
{"name": "endsIndex"}
]
},
{
"name": "StringSubstring",
"main": "run.js",
......
// Copyright 2018 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.
function benchy(fn, name) {
new BenchmarkSuite(name, [1], [
new Benchmark(name, true, false, 0, fn),
]);
}
const inputs = [
'I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9\uFFFD',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
'Integer eu augue suscipit, accumsan ipsum nec, sagittis sem.',
'In vitae pellentesque dolor. Curabitur leo nunc, luctus vitae',
'risus eget, fermentum hendrerit justo.',
];
const first = 'I';
function helper(fn) {
let sum = 0;
for (const input of inputs) {
sum += fn(input);
}
return sum;
}
function startsWith(string) {
return string.startsWith(first);
}
function startsIndex(string) {
return string[0] === first;
}
function endsWith(string) {
return string.endsWith(first);
}
function endsIndex(string) {
return string[string.length - 1] === first;
}
benchy(() => helper(startsWith), 'startsWith');
benchy(() => helper(startsIndex), 'startsIndex');
benchy(() => helper(endsWith), 'endsWith');
benchy(() => helper(endsIndex), 'endsIndex');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment