Skip to content

Instantly share code, notes, and snippets.

@caitp caitp/string-iterator.js
Last active Sep 23, 2016

Embed
What would you like to do?
v8 microbenchmarks for string iterators (https://codereview.chromium.org/2358263002)
// Copyright 2016 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.
new BenchmarkSuite('StringIterators-Spread_OneByteShort', [1000], [
new Benchmark('test', false, false, 0,
Spread_OneByteShort, Spread_OneByteShortSetup,
Spread_OneByteShortTearDown),
]);
var result;
var string;
function Spread_OneByteShortSetup() {
result = undefined;
string = "Alphabet";
string += "-";
string += "Soup";
}
function Spread_OneByteShort() {
result = [...string];
}
function Spread_OneByteShortTearDown() {
var expected = "A|l|p|h|a|b|e|t|-|S|o|u|p";
if (!Array.isArray(result) || result.join("|") !== expected)
throw new Error("Test is broken");
return true;
}
// ----------------------------------------------------------------------------
new BenchmarkSuite('StringIterators-Spread_TwoByteShort', [1000], [
new Benchmark('test', false, false, 0,
Spread_TwoByteShort, Spread_TwoByteShortSetup,
Spread_TwoByteShortTearDown),
]);
var result;
var string;
function Spread_TwoByteShortSetup() {
result = undefined;
string = "\u5FCD\u8005\u306E\u653B\u6483";
}
function Spread_TwoByteShort() {
result = [...string];
}
function Spread_TwoByteShortTearDown() {
var expected = "\u5FCD|\u8005|\u306E|\u653B|\u6483";
if (!Array.isArray(result) || result.join("|") !== expected)
throw new Error("Test is broken");
return true;
}
// ----------------------------------------------------------------------------
new BenchmarkSuite('StringIterators-Spread_WithSurrogatePairsShort', [1000], [
new Benchmark('test', false, false, 0,
Spread_WithSurrogatePairsShort,
Spread_WithSurrogatePairsShortSetup,
Spread_WithSurrogatePairsShortTearDown),
]);
function Spread_WithSurrogatePairsShortSetup() {
result = undefined;
string = "\uD83C\uDF1F\u5FCD\u8005\u306E\u653B\u6483\uD83C\uDF1F";
}
function Spread_WithSurrogatePairsShort() {
result = [...string];
}
function Spread_WithSurrogatePairsShortTearDown() {
var expected =
"\uD83C\uDF1F|\u5FCD|\u8005|\u306E|\u653B|\u6483|\uD83C\uDF1F";
if (!Array.isArray(result) || result.join("|") !== expected)
throw new Error("Test is broken");
return true;
}
// ----------------------------------------------------------------------------
new BenchmarkSuite('StringIterators-ForOf_OneByteLong', [1000], [
new Benchmark('test', false, false, 0,
ForOf_OneByteLong, ForOf_OneByteLongSetup,
ForOf_OneByteLongTearDown),
]);
var result;
var string;
function ForOf_OneByteLongSetup() {
result = undefined;
string = "Alphabet-Soup|".repeat(128);
}
function ForOf_OneByteLong() {
result = "";
for (var c of string) result += c;
}
function ForOf_OneByteLongTearDown() {
if (string != result)
throw new Error(`Test is broken: Expected "${string}" but got ` +
`"${result}"`);
return true;
}
// ----------------------------------------------------------------------------
new BenchmarkSuite('StringIterators-ForOf_TwoByteLong', [1000], [
new Benchmark('test', false, false, 0,
ForOf_OneByteLong, ForOf_OneByteLongSetup,
ForOf_OneByteLongTearDown),
]);
function ForOf_TwoByteLongSetup() {
result = undefined;
string = "\u5FCD\u8005\u306E\u653B\u6483".repeat(128);
}
function ForOf_TwoByteLong() {
result = "";
for (var c of string) result += c;
}
function ForOf_TwoByteLongTearDown() {
if (string != result)
throw new Error(`Test is broken: Expected "${string}" but got ` +
`"${result}"`);
return true;
}
// ----------------------------------------------------------------------------
new BenchmarkSuite('StringIterators-ForOf_WithSurrogatePairsLong', [1000], [
new Benchmark('test', false, false, 0,
ForOf_WithSurrogatePairsLong, ForOf_WithSurrogatePairsLongSetup,
ForOf_WithSurrogatePairsLongTearDown),
]);
var result;
var string;
function ForOf_WithSurrogatePairsLongSetup() {
result = undefined;
string = "\uD83C\uDF1F\u5FCD\u8005\u306E\u653B\u6483\uD83C\uDF1F|"
.repeat(128);
}
function ForOf_WithSurrogatePairsLong() {
result = "";
for (var c of string) result += c;
}
function ForOf_WithSurrogatePairsLongTearDown() {
if (string !== result)
throw new Error(`Test is broken: Expected "${string}" but got ` +
`"${result}"`);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.