diff --git a/.travis.yml b/.travis.yml index 4460917..486acc4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,7 @@ language: node_js +os: windows +install: npm install -g yarn && yarn node_js: - "node" - - "lts/*" \ No newline at end of file + - "lts/*" +filter_secrets: false \ No newline at end of file diff --git a/README.md b/README.md index 20fc8ef..28d203a 100644 --- a/README.md +++ b/README.md @@ -228,23 +228,22 @@ FRS-replace a b --content abcd -o foo_replaced.js FRS-replace a b -i foo.js | ``` -## Benchmarks +## Benchmarks (Node v10.11.0) #### input as glob pattern [1000 iterations x 100 repetitions] | Library (best bolded) | Execution time [s] | Difference percentage (comparing to best time) | | --- | --- | --- | -| **FRS-replace async** | 0.07656150 | 0.0000% | -| FRS-replace sync | 0.31196953 | 307.4757% | -| replace-in-file | 0.76240075 | 895.8017% | -| replace async | 0.11774627 | 53.7931% | -| replace sync | 0.91518713 | 1095.3620% | +| **FRS-replace async** | 0.09039627 | 0.0000% | +| FRS-replace sync | 0.34133441 | 277.5979% | +| replace-in-file | 0.77766898 | 760.2888% | +| replace async | 0.12272055 | 35.7584% | +| replace sync | 0.85676020 | 847.7827% | | replace-string | *N/A* | *N/A* | #### input & replacement as strings [1000 iterations x 100 repetitions] | Library (best bolded) | Execution time [s] | Difference percentage (comparing to best time) | | --- | --- | --- | -| FRS-replace async | 0.00511845 | 77.4972% | -| **FRS-replace sync** | 0.00288368 | 0.0000% | +| FRS-replace async | 0.01194503 | 134.0488% | +| **FRS-replace sync** | 0.00510365 | 0.0000% | | replace-in-file | *N/A* | *N/A* | | replace async | *N/A* | *N/A* | | replace sync | *N/A* | *N/A* | -| replace-string | 0.00292622 | 1.4752% | - +| replace-string | 0.00655492 | 28.4359% | diff --git a/benchmark/replace.spec.js b/benchmark/replace.spec.js index eb26fa8..7c492f1 100644 --- a/benchmark/replace.spec.js +++ b/benchmark/replace.spec.js @@ -38,8 +38,6 @@ const testedLibraries = [ let dir, output, input -const readmeContent = fs.readFileSync('./README.md').toString() - let perfyResults = '' { @@ -133,13 +131,13 @@ tap.test(`input as glob pattern [${iterationsNo} iterations x ${repetitionsNo / }, void 0 ]) - const sortedResults = results.slice().sort(sortByNanoseconds) + + const result = outputPerfy(ct, results, results.slice().sort(sortByNumberVariable('fullNanoseconds'))[0]) + const sortedResults = result.results.slice().sort(sortByNumberVariable('avgTime')) ct.is((sortedResults[0].name.indexOf('FRS-replace') !== -1 || (sortedResults[1].name.indexOf('FRS-replace') !== -1 && sortedResults[1].avgPercentageDifference < 5)), true, 'FRS-replace should be the fastest or second, but at most with 5% difference to best') ct.not(sortedResults[2].name.indexOf('FRS-replace sync'), -1, 'FRS-replace sync should be third (right after async replace)') - outputPerfy(ct, results, sortedResults[0]) - ct.end() }) @@ -165,9 +163,8 @@ tap.test(`input & replacement as strings [${iterationsNo} iterations x ${repetit { fn: () => replaceString(content, regex.source, replacement) } ]) - const result = outputPerfy(ct, results, results.slice().sort(sortByNanoseconds)[0]) - - const sortedResults = result.results.slice().sort(sortByNanoseconds) + const result = outputPerfy(ct, results, results.slice().sort(sortByNumberVariable('fullNanoseconds'))[0]) + const sortedResults = result.results.slice().sort(sortByNumberVariable('avgTime')) ct.is((sortedResults[0].name.indexOf('FRS-replace') !== -1 || (sortedResults[1].name.indexOf('FRS-replace') !== -1 && sortedResults[1].avgPercentageDifference < 10)), true, 'FRS-replace should be the fastest or second, but at most with 10% difference to best') @@ -175,7 +172,9 @@ tap.test(`input & replacement as strings [${iterationsNo} iterations x ${repetit }) tap.teardown(() => { - fs.writeFileSync('./README.md', readmeContent.replace(/(##\sBenchmarks\s\s)[\s\S]*?(?:$|(?:\s##\s))/, '$1' + perfyResults)) + const readmeContent = fs.readFileSync('./README.md').toString() + + fs.writeFileSync('./README.md', readmeContent.replace(/(##\sBenchmarks \(Node )(?:.*?)(\)\s\s)[\s\S]*?(?:$|(?:\s##\s))/, `$1${process.version}$2${perfyResults}`)) }) function outputPerfy (t, testResults, best) { @@ -305,14 +304,19 @@ async function singleTest (name, test, n) { return result } -function sortByNanoseconds (a, b) { - if (a.fullNanoseconds === void 0) { - return b.fullNanoseconds === void 0 ? 0 : 1 - } +function sortByNumberVariable (varName) { + return (a, b) => { + a = a[varName] + b = b[varName] - if (b.fullNanoseconds === void 0) { - return -1 - } + if (a === void 0 || a === null) { + return b === void 0 || b === null ? 0 : 1 + } - return a.fullNanoseconds - b.fullNanoseconds + if (b === void 0 || b === null) { + return -1 + } + + return a - b + } } diff --git a/package.json b/package.json index 5f0e9e4..f98c86d 100644 --- a/package.json +++ b/package.json @@ -39,10 +39,11 @@ "release": "standard-version", "postrelease": "git push --follow-tags origin master && yarn publish", "pretest": "standard", - "test": "yarn test:unit --100 --coverage-report=html && yarn test:benchmark", - "pretest:unit": "standard --fix", - "test:unit": "tap ./src/*.spec.js ./bin/*.spec.js -J", - "test:benchmark": "tap ./benchmark/*.spec.js --no-timeout" + "test": "yarn test:unit && tap --coverage-report=html", + "posttest": "yarn test:benchmark", + "test:unit": "tap ./src/*.spec.js ./bin/*.spec.js -J --no-timeout", + "test:benchmark": "tap ./benchmark/*.spec.js --no-timeout", + "standard": "standard --fix" }, "nyc": { "exclude": "**/*.spec.js", diff --git a/src/replace.js b/src/replace.js index 84167e2..7642007 100644 --- a/src/replace.js +++ b/src/replace.js @@ -63,9 +63,9 @@ async function replaceAsync ({ const replaceFn = typeof regex === 'string' ? replaceString : replaceRegex if (content !== void 0) { - result = replaceFn(content, regex, replacement) + result = new Promise(resolve => resolve(replaceFn(content, regex, replacement))) } else if (input !== void 0) { - const fileStream = await require('fast-glob').stream(input, inputGlobOptions) + const fileStream = require('fast-glob').stream(input, inputGlobOptions) let filesFound = false result = '' @@ -81,9 +81,7 @@ async function replaceAsync ({ fileStream.once('error', writeError) }) - await new Promise((resolve) => fileStream.once('end', () => { - return resolve(filesFound ? fileReaderPromise : void 0) - })) + await new Promise(resolve => fileStream.once('end', () => resolve(filesFound ? fileReaderPromise : void 0))) } else { writeError('at least one input source must be defined!') }