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: ['undefined', 'utf8', 'ucs2'], 23 type: ['buffer', 'string'], 24 n: [5e4], 25}, { 26 combinationFilter: (p) => { 27 return (p.type === 'buffer' && p.encoding === 'undefined') || 28 (p.type !== 'buffer' && p.encoding !== 'undefined'); 29 }, 30}); 31 32function main({ n, search, encoding, type }) { 33 let aliceBuffer = fs.readFileSync( 34 path.resolve(__dirname, '../fixtures/alice.html'), 35 ); 36 37 if (encoding === 'undefined') { 38 encoding = undefined; 39 } 40 41 if (encoding === 'ucs2') { 42 aliceBuffer = Buffer.from(aliceBuffer.toString(), encoding); 43 } 44 45 if (type === 'buffer') { 46 search = Buffer.from(Buffer.from(search).toString(), encoding); 47 } 48 49 bench.start(); 50 for (let i = 0; i < n; i++) { 51 aliceBuffer.indexOf(search, 0, encoding); 52 } 53 bench.end(n); 54} 55