Created
December 15, 2016 19:41
-
-
Save cevek/6a6eb3872e4e1f858039b006dfead74a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var js = fs.readFileSync('small.js', 'utf-8'); | |
// performance: | |
// 10mb of code - 50ms | |
function find(js) { | |
var skipBlocks = []; | |
var k = 0; | |
js = js + '\n'; | |
for (var i = 0; i < js.length; i++) { | |
var code = js.charCodeAt(i); | |
if (code === 47) { | |
var nextcode = js.charCodeAt(++i); | |
if (nextcode === 42) { | |
var start = i - 1; | |
while (nextcode = js.charCodeAt(++i)) { | |
if (nextcode === 42) { | |
if (js.charCodeAt(i + 1) === 47) { | |
i++; | |
skipBlocks[k] = start; | |
k++; | |
skipBlocks[k] = i + 1; | |
k++; | |
break; | |
} | |
} | |
} | |
} else if (nextcode === 47 && js.charCodeAt(i - 1) !== 92) { | |
start = i - 1; | |
while (nextcode = js.charCodeAt(++i)) { | |
if (nextcode === 10) { | |
skipBlocks[k] = start; | |
k++; | |
skipBlocks[k] = i + 1; | |
k++; | |
break; | |
} | |
} | |
} | |
} else if (code === 34 || code === 39) { | |
start = i; | |
while (nextcode = js.charCodeAt(++i)) { | |
if (nextcode === 92) { | |
i++; | |
continue; | |
} | |
if (nextcode === code) { | |
skipBlocks[k] = start; | |
k++; | |
skipBlocks[k] = i + 1; | |
k++; | |
break; | |
} | |
} | |
} | |
} | |
// var comments = js.match(/\/\*[\s\S]*\*\//g); | |
// var lineComments = js.match(/\/\/.*\n/g); | |
// var strings = js.match(/(?=["'])(?:"[^"\\]*(?:\\[\s\S][^"\\]*)*"|'[^'\\]*(?:\\[\s\S][^'\\]*)*')/g); | |
// var requres = js.match(/_dereq_\(\s*["']?(.*?)["']?\s*\)/g); | |
return skipBlocks; | |
} | |
var hot = '/*1*/ //1 \n"1" \'a\''; | |
for (let i = 0; i < 500; i++) { | |
var res = find(hot); | |
} | |
for (let i = 0; i < 10; i++) { | |
console.time('perf'); | |
var res = find(js); | |
console.timeEnd('perf'); | |
} | |
console.log(res.length); | |
for (let i = 0; i < res.length; i += 2) { | |
// console.log('------', hot.substring(res[i], res[i + 1])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment