• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const fs = require('fs');
4const path = require('path');
5
6const searchStrings = [
7  '@',
8  'SQ',
9  '--l',
10  'Alice',
11  'Gryphon',
12  'Ou est ma chatte?',
13  'found it very',
14  'neighbouring pool',
15  'aaaaaaaaaaaaaaaaa',
16  'venture to go near the house till she had brought herself down to',
17  '</i> to the Caterpillar',
18];
19
20const bench = common.createBenchmark(main, {
21  search: searchStrings,
22  encoding: ['utf8', 'ucs2'],
23  type: ['buffer', 'string'],
24  n: [5e4]
25});
26
27function main({ n, search, encoding, type }) {
28  let aliceBuffer = fs.readFileSync(
29    path.resolve(__dirname, '../fixtures/alice.html')
30  );
31
32  if (encoding === 'undefined') {
33    encoding = undefined;
34  }
35
36  if (encoding === 'ucs2') {
37    aliceBuffer = Buffer.from(aliceBuffer.toString(), encoding);
38  }
39
40  if (type === 'buffer') {
41    search = Buffer.from(Buffer.from(search).toString(), encoding);
42  }
43
44  bench.start();
45  for (let i = 0; i < n; i++) {
46    aliceBuffer.indexOf(search, 0, encoding);
47  }
48  bench.end(n);
49}
50