Created
November 10, 2015 08:55
-
-
Save antonfisher/95bb1b84d87fad6b9ad4 to your computer and use it in GitHub Desktop.
Build in Nodejs split function VS bash 'sed & tr' command
This file contains 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
/** | |
* Usage: | |
* $npm install shelljs | |
* $node split-vs-sed.js | |
*/ | |
/** | |
* Results: | |
* | |
* $ node split-vs-sed.js | |
* Node split() time: 6106ms | |
* Tr + Sed time: 6324ms | |
* Sed regexp time: 6251ms | |
*/ | |
var shelljs = require('shelljs'); | |
var exec = shelljs.exec; | |
shelljs.config.silent = true; | |
exec('mkdir -p /tmp/disks/0/configs && echo \'"id": "10ecc78a910-"\' | dd of=/tmp/disks/0/configs/ctr.json'); | |
exec('mkdir -p /tmp/disks/1/configs && echo \'"id": "10ecc78a9106"\' | dd of=/tmp/disks/1/configs/ctr.json'); | |
exec('mkdir -p /tmp/disks/2/configs && echo \'"id": "10ecc78a910+"\' | dd of=/tmp/disks/2/configs/ctr.json'); | |
var command = 'find /tmp/disks/* -type f -name ctr.json -exec grep -sl \'"id": "10ecc78a9106"\' {} \\;'; | |
var commandSed = '| tr "/" "\\n" | sed -n 4p | tr -d "\\n"'; | |
var commandSedRegExp = '| sed \'s#^[^0-9]*\\([0-9]*\\).*$#\\1#\' | tr -d "\\n"'; | |
var count = 100; | |
console.log('Node split() time [ ' + command + ' ]:'); | |
console.time(' '); | |
for (var i = 0; i < count; i++) { | |
process.stdout.write(exec(command).output.split('/')[3]); | |
} | |
console.timeEnd(' '); | |
console.log('Tr + Sed time [ ' + command + ' ' + commandSed + ' ]:'); | |
console.time(' '); | |
for (var i = 0; i < count; i++) { | |
process.stdout.write(exec(command + commandSed).output); | |
} | |
console.timeEnd(' '); | |
console.log('Sed regexp time [ ' + command + ' ' + commandSedRegExp + ' ]:'); | |
console.time(' '); | |
for (var i = 0; i < count; i++) { | |
process.stdout.write(exec(command + commandSedRegExp).output); | |
} | |
console.timeEnd(' '); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment