Last active
August 29, 2015 14:10
-
-
Save miku/31864156938fcd0c8430 to your computer and use it in GitHub Desktop.
eachline.js - http://stackoverflow.com/q/27090124/89391
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
#!/usr/bin/env node | |
// eachline.js - execute some js per line in a file | |
// | |
// $ time eachline.js datasets/1M.ldj > /dev/null | |
// | |
// real 0m25.211s | |
// user 0m24.126s | |
// sys 0m2.171s | |
var fs = require('fs'), | |
readline = require('readline'); | |
vm = require('vm'); | |
var args = process.argv.slice(2); | |
if (args.length < 1) { | |
console.log("Usage: eachline.js FILENAME") | |
process.exit(code=1) | |
} | |
var rd = readline.createInterface({ | |
input: fs.createReadStream(args[0]), | |
output: process.stdout, | |
terminal: false | |
}); | |
var context = vm.createContext({}); | |
var script = vm.createScript('(function (input) { return input.length; })'); | |
var fn = script.runInContext(context); | |
rd.on('line', function(line) { | |
console.log(fn(line)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment