Skip to content

Instantly share code, notes, and snippets.

@miku
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miku/31864156938fcd0c8430 to your computer and use it in GitHub Desktop.
Save miku/31864156938fcd0c8430 to your computer and use it in GitHub Desktop.
#!/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