title: node.js dynamic javascript execution performance
body: <p>I'd like to execute an arbitrary - user supplied - piece of javascript code on each line of a file. I created a simple node command line application, that will - as an example - just take each line and will output its length:</p>
  
  <pre><code>#!/usr/bin/env node
  // eachline.js - execute some js per line in a file
  
  var fs = require('fs'),
      readline = require('readline');
      vm = require('vm');
  
  var args = process.argv.slice(2);
  
  if (args.length &lt; 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({});
  // this snippet should be user supplied, arbitrary javascript
  var script = vm.createScript('output = input.length');
  
  rd.on('line', function(line) {
      context.input = line;
      script.runInContext(context);
      console.log(context.output);
  });
  </code></pre>
  
  <p>The performance is not optimal. For a file with 1 million lines, it takes several minutes:</p>
  
  <pre><code>$ time ./eachline.js 1M.txt &gt; /dev/null
  
  real    4m14.366s
  user    4m12.200s
  sys     0m4.545s
  </code></pre>
  
  <p>With <code>awk</code> this simple operation takes about five seconds. With a <a href="https://github.com/miku/ottily#performance" rel="nofollow">similar program</a>, that executes arbitrary javascript a bit more than ten seconds.</p>
  
  <p>I haven't worked with node before, so the program above is my first uneducated guess at a solution. How could I improve performance?</p>
  
  <p>Edit: Above source code in a gist: <a href="https://gist.github.com/miku/31864156938fcd0c8430" rel="nofollow">https://gist.github.com/miku/31864156938fcd0c8430</a></p>
  
  <hr>
  
  <p>PS. Goal is to have a program that can run arbitrary javascript on each line of a file fast.</p>