11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common.js'); 31cb0ef41Sopenharmony_ciconst fs = require('fs'); 41cb0ef41Sopenharmony_ciconst path = require('path'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst searchStrings = [ 71cb0ef41Sopenharmony_ci '@', 81cb0ef41Sopenharmony_ci 'SQ', 91cb0ef41Sopenharmony_ci '--l', 101cb0ef41Sopenharmony_ci 'Alice', 111cb0ef41Sopenharmony_ci 'Gryphon', 121cb0ef41Sopenharmony_ci 'Ou est ma chatte?', 131cb0ef41Sopenharmony_ci 'found it very', 141cb0ef41Sopenharmony_ci 'neighbouring pool', 151cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaa', 161cb0ef41Sopenharmony_ci 'venture to go near the house till she had brought herself down to', 171cb0ef41Sopenharmony_ci '</i> to the Caterpillar', 181cb0ef41Sopenharmony_ci]; 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 211cb0ef41Sopenharmony_ci search: searchStrings, 221cb0ef41Sopenharmony_ci encoding: ['undefined', 'utf8', 'ucs2'], 231cb0ef41Sopenharmony_ci type: ['buffer', 'string'], 241cb0ef41Sopenharmony_ci n: [5e4], 251cb0ef41Sopenharmony_ci}, { 261cb0ef41Sopenharmony_ci combinationFilter: (p) => { 271cb0ef41Sopenharmony_ci return (p.type === 'buffer' && p.encoding === 'undefined') || 281cb0ef41Sopenharmony_ci (p.type !== 'buffer' && p.encoding !== 'undefined'); 291cb0ef41Sopenharmony_ci }, 301cb0ef41Sopenharmony_ci}); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_cifunction main({ n, search, encoding, type }) { 331cb0ef41Sopenharmony_ci let aliceBuffer = fs.readFileSync( 341cb0ef41Sopenharmony_ci path.resolve(__dirname, '../fixtures/alice.html'), 351cb0ef41Sopenharmony_ci ); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci if (encoding === 'undefined') { 381cb0ef41Sopenharmony_ci encoding = undefined; 391cb0ef41Sopenharmony_ci } 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci if (encoding === 'ucs2') { 421cb0ef41Sopenharmony_ci aliceBuffer = Buffer.from(aliceBuffer.toString(), encoding); 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci if (type === 'buffer') { 461cb0ef41Sopenharmony_ci search = Buffer.from(Buffer.from(search).toString(), encoding); 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci bench.start(); 501cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) { 511cb0ef41Sopenharmony_ci aliceBuffer.indexOf(search, 0, encoding); 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci bench.end(n); 541cb0ef41Sopenharmony_ci} 55