Home | History | Annotate | Download | only in Strings
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 new BenchmarkSuite('StringFunctions', [1000], [
      6   new Benchmark('StringRepeat', false, false, 0,
      7                 Repeat, RepeatSetup, RepeatTearDown),
      8   new Benchmark('StringStartsWith', false, false, 0,
      9                 StartsWith, WithSetup, WithTearDown),
     10   new Benchmark('StringEndsWith', false, false, 0,
     11                 EndsWith, WithSetup, WithTearDown),
     12   new Benchmark('StringIncludes', false, false, 0,
     13                 Includes, IncludesSetup, WithTearDown),
     14   new Benchmark('StringFromCodePoint', false, false, 0,
     15                 FromCodePoint, FromCodePointSetup, FromCodePointTearDown),
     16   new Benchmark('StringCodePointAt', false, false, 0,
     17                 CodePointAt, CodePointAtSetup, CodePointAtTearDown),
     18 ]);
     19 
     20 
     21 var result;
     22 
     23 var stringRepeatSource = "abc";
     24 
     25 function RepeatSetup() {
     26   result = undefined;
     27 }
     28 
     29 function Repeat() {
     30   result = stringRepeatSource.repeat(500);
     31 }
     32 
     33 function RepeatTearDown() {
     34   var expected = "";
     35   for(var i = 0; i < 1000; i++) {
     36     expected += stringRepeatSource;
     37   }
     38   return result === expected;
     39 }
     40 
     41 
     42 var str;
     43 var substr;
     44 
     45 function WithSetup() {
     46   str = "abc".repeat(500);
     47   substr = "abc".repeat(200);
     48   result = undefined;
     49 }
     50 
     51 function WithTearDown() {
     52   return !!result;
     53 }
     54 
     55 function StartsWith() {
     56   result = str.startsWith(substr);
     57 }
     58 
     59 function EndsWith() {
     60   result = str.endsWith(substr);
     61 }
     62 
     63 function IncludesSetup() {
     64   str = "def".repeat(100) + "abc".repeat(100) + "qqq".repeat(100);
     65   substr = "abc".repeat(100);
     66 }
     67 
     68 function Includes() {
     69   result = str.includes(substr);
     70 }
     71 
     72 var MAX_CODE_POINT = 0xFFFFF;
     73 
     74 function FromCodePointSetup() {
     75   result = new Array(MAX_CODE_POINT + 1);
     76 }
     77 
     78 function FromCodePoint() {
     79   for (var i = 0; i <= MAX_CODE_POINT; i++) {
     80     result[i] = String.fromCodePoint(i);
     81   }
     82 }
     83 
     84 function FromCodePointTearDown() {
     85   for (var i = 0; i <= MAX_CODE_POINT; i++) {
     86     if (i !== result[i].codePointAt(0)) return false;
     87   }
     88   return true;
     89 }
     90 
     91 
     92 var allCodePoints;
     93 
     94 function CodePointAtSetup() {
     95   allCodePoints = new Array(MAX_CODE_POINT + 1);
     96   for (var i = 0; i <= MAX_CODE_POINT; i++) {
     97     allCodePoints = String.fromCodePoint(i);
     98   }
     99   result = undefined;
    100 }
    101 
    102 function CodePointAt() {
    103   result = 0;
    104   for (var i = 0; i <= MAX_CODE_POINT; i++) {
    105     result += allCodePoints.codePointAt(i);
    106   }
    107 }
    108 
    109 function CodePointAtTearDown() {
    110   return result === MAX_CODE_POINT * (MAX_CODE_POINT + 1) / 2;
    111 }
    112