string-indexof.js 854 Bytes
Newer Older
1 2 3 4
// Copyright 2017 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.

5
new BenchmarkSuite('StringIndexOfConstant', [5], [
6 7 8 9
  new Benchmark('StringIndexOfConstant', true, false, 0,
  StringIndexOfConstant),
]);

10
new BenchmarkSuite('StringIndexOfNonConstant', [5], [
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
  new Benchmark('StringIndexOfNonConstant', true, false, 0,
  StringIndexOfNonConstant),
]);

const subject = "aaaaaaaaaaaaaaaab";
const searches = ['a', 'b', 'c'];

function StringIndexOfConstant() {
  var sum = 0;

  for (var j = 0; j < searches.length; ++j) {
    sum += subject.indexOf("" + searches[j]);
  }

  return sum;
}

function StringIndexOfNonConstant() {
  var sum = 0;

  for (var j = 0; j < searches.length; ++j) {
    sum += subject.indexOf(searches[j]);
  }

  return sum;
}