summaryrefslogtreecommitdiff
path: root/deps/v8/test/js-perf-test/Strings/harmony-string.js
blob: c2eac4ee33d4f4578b3f34f5fbbc5c35130d9033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2014 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('StringFunctions', [1000], [
  new Benchmark('StringRepeat', false, false, 0,
                Repeat, RepeatSetup, RepeatTearDown),
  new Benchmark('StringStartsWith', false, false, 0,
                StartsWith, WithSetup, WithTearDown),
  new Benchmark('StringEndsWith', false, false, 0,
                EndsWith, WithSetup, WithTearDown),
  new Benchmark('StringIncludes', false, false, 0,
                Includes, IncludesSetup, WithTearDown),
  new Benchmark('StringFromCodePoint', false, false, 0,
                FromCodePoint, FromCodePointSetup, FromCodePointTearDown),
  new Benchmark('StringCodePointAt', false, false, 0,
                CodePointAt, CodePointAtSetup, CodePointAtTearDown),
]);


var result;

var stringRepeatSource = "abc";

function RepeatSetup() {
  result = undefined;
}

function Repeat() {
  result = stringRepeatSource.repeat(500);
}

function RepeatTearDown() {
  var expected = "";
  for(var i = 0; i < 1000; i++) {
    expected += stringRepeatSource;
  }
  return result === expected;
}


var str;
var substr;

function WithSetup() {
  str = "abc".repeat(500);
  substr = "abc".repeat(200);
  result = undefined;
}

function WithTearDown() {
  return !!result;
}

function StartsWith() {
  result = str.startsWith(substr);
}

function EndsWith() {
  result = str.endsWith(substr);
}

function IncludesSetup() {
  str = "def".repeat(100) + "abc".repeat(100) + "qqq".repeat(100);
  substr = "abc".repeat(100);
}

function Includes() {
  result = str.includes(substr);
}

var MAX_CODE_POINT = 0xFFFFF;

function FromCodePointSetup() {
  result = new Array(MAX_CODE_POINT + 1);
}

function FromCodePoint() {
  for (var i = 0; i <= MAX_CODE_POINT; i++) {
    result[i] = String.fromCodePoint(i);
  }
}

function FromCodePointTearDown() {
  for (var i = 0; i <= MAX_CODE_POINT; i++) {
    if (i !== result[i].codePointAt(0)) return false;
  }
  return true;
}


var allCodePoints;

function CodePointAtSetup() {
  allCodePoints = new Array(MAX_CODE_POINT + 1);
  for (var i = 0; i <= MAX_CODE_POINT; i++) {
    allCodePoints = String.fromCodePoint(i);
  }
  result = undefined;
}

function CodePointAt() {
  result = 0;
  for (var i = 0; i <= MAX_CODE_POINT; i++) {
    result += allCodePoints.codePointAt(i);
  }
}

function CodePointAtTearDown() {
  return result === MAX_CODE_POINT * (MAX_CODE_POINT + 1) / 2;
}