Node FTW

tl;dr

Want to crunch some numbers? Node is 10x faster than Ruby.

I used to do a lot of scientific computing. My work environment had both Matlab and NumPy/SciPy all tricked out. These days I don’t do too much number crunching, spending most of my time with Ruby and JavaScript.

Recently I’ve been interested in the math of split testing so what do you think I turned to when it was time to do some simulations? That’s right, Ruby… I didn’t really think about what I was doing before I jumped in.

I had reached for Ruby even though the I coding up a numerical simulation instead of some web app. I wanted to simulate 100,000 coin flips as one trial, and then repeat it for a bunch of trials to decrease the statistical sampling error. Here’s the results:

$ ruby aa.rb 100

Trials: 100

Total Time: 21.243 seconds

But I needed more accuracy

$ ruby aa.rb 300

Trials: 300

Total Time: 62.260 seconds

Still need more accuracy
$ ruby aa.rb 1000

Trials: 1000

Total Time: 208.607 seconds

This is ridiculous. While I was waiting for the last simulation to finish I started thinking about setting up MATLAB or NumPy. Then it hit me. Why did I reach for Ruby when I could have coded in JavaScript?! JavaScript is a scripting language that I’m intimately acquainted with, so it takes me roughly the same amount of time to code in JavaScript as it does to code in Ruby. But what a difference in runtime:

$ node aa.js 100

Trials: 100

Total time: 1.855 seconds

$ node aa.js 300

Trials: 300

Total time: 5.486 seconds

$ node aa.js 1000

Trials: 1000

Total time: 17.726 seconds

10x faster than Ruby.

So thanks Node/Google/V8, for making JavaScript awesome!

EDIT: I just ran the JavaScript version of the script in the Chrome console and it still beat Ruby by 30%.

 
13
Kudos
 
13
Kudos

Now read this

I Coded the Angular Tutorial App in Backbone and it Took 260% More Code

tl;dr The people at AngularJS created their PhoneCat tutorial app with 48 lines of JavaScript . When we coded the same app using Backbone instead of Angular, we found it took 171 lines of JavaScript – 260% more code. Here, we present a... Continue →