Improve alphabet bruteforcer
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent 21b99f7 · Browse files · 3 files·+18 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+18 added−26 removed
- tests:+16 added−25 removed
- other:+2 added−1 removed
Provenance
- Commit
a1ca28b- Tree
47e5ea6- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
21b99f7
3 files changed+18 added−26 removed
modified:test/alphabet+7 added-22 removed
33 unmodified lines34 }35 36 var newline = false37 38-var all = js.length * js.length * js.length38+var all = js.length * (js.length + 1) * (js.length + 1)39 var step = 040 var lastPart = 041 42 function print (str) {43- process.stdout.write(str)44- newline = true45-}46-47-function printLine (str) {48- if (newline) process.stdout.write('\n')49- newline = false43 process.stdout.write(str + '\n')44 }45 46 new Promise(function (resolve) {10 unmodified lines57 worker.on('message', function (data) {58 if (data.finished) {59 finished += 160 if (finished === WORKERS) {68- printLine(69- chalk.bold(step) + ' variants were checked. The best alphabet:\n' +70- chalk.green(best.alphabet)71- )61+ print('The best alphabet:\n' + chalk.bold.green(best.alphabet))62 resolve()63 }64 } else {75- if (data.size && data.size < best.size) {65+ if (data.size < best.size) {66 if (newline) process.stdout.write('\n')67 newline = false68 best = data79- printLine(best.alphabet)69+ print(best.alphabet)70 }81- step += 182- var part = Math.floor(80 * 100 * step / all)71+ step += data.steps72+ var part = Math.floor(100 * step / all)73 if (part > lastPart) {74 lastPart = part85- if (part % 80 === 0) {86- print(chalk.gray('.'))75+ print(chalk.gray(part + '%'))87- printLine(chalk.gray((part / 80) + '%'))88- } else {89- print(chalk.gray('.'))90- }76 }77 }78 })79 worker.on('error', function (e) {4 unmodified linesmodified:test/alphabet-worker.js+9 added-3 removed
5 unmodified lines6789101112131412 unmodified lines27282930303132333435363738383940414243444546474849505152535455565758596061626364656667686963707172732 unmodified lines5 unmodified lines
function getAlphabet (positions) { var alphabet = '' positions.forEach(function (pos) { if (pos === -1) return while (pos < workerData.js.length) { var char = workerData.js[pos] if (workerData.alphabet.indexOf(char) === -1) break if (alphabet.indexOf(char) !== -1) break12 unmodified lines } return alphabet}
var positions = [workerData.start, 0, 0]var positions = [workerData.start, -1, -1]
function increase () { positions[2] += 1 if (positions[2] >= workerData.js.length) { positions[2] = 0 positions[1] += 1 if (positions[1] >= workerData.js.length) { positions[1] = 0 positions[2] = -1 positions[1] = -1 positions[0] += workerData.step if (positions[0] >= workerData.js.length) { return false } } } return true}
var steps = 0
function tick () { if (!increase(positions)) { parentPort.postMessage({ finished: true }) } else { steps += 1 var alphabet = getAlphabet(positions) if (alphabet) { var file = workerData.js.replace(/[A-Za-z0-9~_]{30,}/, alphabet) gzipSize(file).then(function (size) { parentPort.postMessage({ alphabet: alphabet, steps: steps, size: size }) steps = 0 tick() }) } else { parentPort.postMessage({ tick: true }) tick() } }}2 unmodified linesmodified:package.json+2 added-1 removed
30 unmodified lines31 "eslint-plugin-node": "^7.0.1",32 "eslint-plugin-promise": "^4.0.1",33 "eslint-plugin-security": "^1.4.0",34 "eslint-plugin-standard": "^4.0.0",35+ "gzip-size": "^5.0.0",36 "html-webpack-plugin": "^3.2.0",37 "jest": "^23.6.0",38 "jsdoc": "^3.5.5",39 "lint-staged": "^7.3.0",14 unmodified lines54 "lint-staged": "lint-staged",55 "spellcheck": "yarn docs && yaspeller-ci *.md docs/*.html",56 "clean": "rimraf docs/ coverage/",57 "docs": "jsdoc -d docs *.js async/*.js",57- "lint": "eslint-ci *.js async/*.js test/{benchmark,bruteforce} test/*.js test/**/*.js",58+ "lint": "eslint-ci *.js async/*.js test/{benchmark,alphabet} test/*.js test/**/*.js",59 "test": "jest --coverage && yarn lint && size-limit && yarn spellcheck",60 "start": "webpack-dev-server --config test/demo/webpack.config"61 },62 "size-limit": [115 unmodified lines