string-iterator.js 5.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// 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('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-Soup";
}

function Spread_OneByteShort() {
  result = [...string];
}

function Spread_OneByteShortTearDown() {
  var expected = "A|l|p|h|a|b|e|t|-|S|o|u|p";
24 25
  assert(Array.isArray(result));
  assertEquals(expected, result.join("|"));
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
}

// ----------------------------------------------------------------------------

new BenchmarkSuite('Spread_TwoByteShort', [1000], [
  new Benchmark('test', false, false, 0,
                Spread_TwoByteShort, Spread_TwoByteShortSetup,
                Spread_TwoByteShortTearDown),
]);

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";
47 48
  assert(Array.isArray(result));
  assertEquals(expected, result.join("|"));
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
}

// ----------------------------------------------------------------------------

new BenchmarkSuite('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";
72 73
  assert(Array.isArray(result));
  assertEquals(expected, result.join("|"));
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
}

// ----------------------------------------------------------------------------

new BenchmarkSuite('ForOf_OneByteShort', [1000], [
  new Benchmark('test', false, false, 0,
                ForOf_OneByteShort, ForOf_OneByteShortSetup,
                ForOf_OneByteShortTearDown),
]);

function ForOf_OneByteShortSetup() {
  result = undefined;
  string = "Alphabet-Soup";
}

function ForOf_OneByteShort() {
  result = "";
  for (var c of string) result += c;
}

function ForOf_OneByteShortTearDown() {
95
  assertEquals(string, result);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}

// ----------------------------------------------------------------------------

new BenchmarkSuite('ForOf_TwoByteShort', [1000], [
  new Benchmark('test', false, false, 0,
                ForOf_TwoByteShort, ForOf_TwoByteShortSetup,
                ForOf_TwoByteShortTearDown),
]);

function ForOf_TwoByteShortSetup() {
  result = undefined;
  string = "\u5FCD\u8005\u306E\u653B\u6483";
}

function ForOf_TwoByteShort() {
  result = "";
  for (var c of string) result += c;
}

function ForOf_TwoByteShortTearDown() {
117
  assertEquals(string, result);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
}

// ----------------------------------------------------------------------------

new BenchmarkSuite('ForOf_WithSurrogatePairsShort', [1000], [
  new Benchmark('test', false, false, 0,
                ForOf_WithSurrogatePairsShort,
                ForOf_WithSurrogatePairsShortSetup,
                ForOf_WithSurrogatePairsShortTearDown),
]);

function ForOf_WithSurrogatePairsShortSetup() {
  result = undefined;
  string = "\uD83C\uDF1F\u5FCD\u8005\u306E\u653B\u6483\uD83C\uDF1F";
}

function ForOf_WithSurrogatePairsShort() {
  result = "";
  for (var c of string) result += c;
}

function ForOf_WithSurrogatePairsShortTearDown() {
140
  assertEquals(string, result);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
}

// ----------------------------------------------------------------------------

new BenchmarkSuite('ForOf_OneByteLong', [1000], [
  new Benchmark('test', false, false, 0,
                ForOf_OneByteLong, ForOf_OneByteLongSetup,
                ForOf_OneByteLongTearDown),
]);

function ForOf_OneByteLongSetup() {
  result = undefined;
  string = "Alphabet-Soup|".repeat(128);
}

function ForOf_OneByteLong() {
  result = "";
  for (var c of string) result += c;
}

function ForOf_OneByteLongTearDown() {
162
  assertEquals(string, result);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}

// ----------------------------------------------------------------------------

new BenchmarkSuite('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() {
184
  assertEquals(string, result);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
}

// ----------------------------------------------------------------------------

new BenchmarkSuite('ForOf_WithSurrogatePairsLong', [1000], [
  new Benchmark('test', false, false, 0,
                ForOf_WithSurrogatePairsLong, ForOf_WithSurrogatePairsLongSetup,
                ForOf_WithSurrogatePairsLongTearDown),
]);

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() {
207
  assertEquals(string, result);
208
}