Tuesday, October 4, 2011

Node.js has jumped the shark

I've been trying to understand this whole Node.js thing that's been going on for the past few days. It's taught me a lot about how the Node.js community tries to reason about problems, and perhaps what's the real problem with Node.

It began with a relatively short rant by Ryan Dahl. Ryan talk's about how he's mad at complex software, which is at least an arguable position. I liked his rant, but Ted Dziuba did not.

Ted Dziuba wrote a brilliant troll post which lambasted Ryan and the concept of Node.js in general. I'm a huge fan of Ted's previous trolling, especially his parody TechCrunch site uncov, which trolled TechCrunch before TechCrunch became a parody of itself. Except in the case of uncov, it would be a few years before TechCrunch became a parody itself. With Node.js, the reaction was almost instantaneous.

Ted's argument centered around the problem of doing non-trivial computation within the event loop of a system like Node, and how that would affect the performance of the system. To illustrate this point, he gave an intentionally deoptimized Fibonacci function, along with the boldface caveat "Yes, I know there's a closed form solution. Shouldn't you be in front of a mirror somewhere, figuring out how to introduce yourself to her?"

Reading through the lines of Ted's trolling, his point is "I'm not specifically talking about Fibonacci. I'm talking about non-trivial computation as a general problem in Node." He specifically chose a slow algorithm to illustrate his point. However, what was the Node community's reaction?

One reaction was to go out and implement Ted's intentionally-retarded Fibonacci algorithm in a bunch of different languages and benchmark how fast the VM could optimize away a crappy algorithm. Based on several benchmarks, V8 actually does a comparatively good way of optimizing away the retarded algorithm compared to a lot of other languages. Cool story bro!

In the Reddit comments for this story I first truly gazed into the abyss of Node.js insanity. Node provides a way to schedule an anonymous function to run the next time it's event loop iterates using a function called nextTick. EventMachine (ugh) users may be familiar with this same pattern with EM.next_tick (which needs to die for most use cases, but that's the subject of another blog post)

Redditor "headzoo" suggests that we provide a "non-blocking solution" by unrolling the Fibonacci loop and execute one iteration of the Node.js event loop, calculating one iteration for each I/O multiplexing system call Node is making. When Ted Dziuba asked "Have you taken epoll into your heart?," I think he had no idea of the depths Node people actually have.

I don't really get what's going on in Node people's heads here. I think they seriously believe that throwing computation into the middle of an event loop is a good idea, because Node's event loop is FAST!!#$#! funroll-loops + nextTick = LUDICROUS SPEED!!

This crap is absolutely insane. For each iteration of the loop, Node is making an expensive system call. It's also defeating all of the mechanisms that V8's impressive JIT would use to optimize this problem, as substantiated by the previously mentioned Nodian's roflbenchmark. Clearly this is a very bad idea. But wait, it gets worse!

It wouldn't take long before another Node.js fan put together a Github project for this technique, which also added an "optimization": memoization! In addition to unrolling each iteration of the Fibonacci function to run within the event loop, it now caches the nth result. Now let's throw ApacheBench at it and see how fast Node can serve the precomputed result where n = 100.

Holy balls, it does 5390.22 requests per second serving a memoized value when I ask ApacheBench to request the same memoized value over and over!!#$! TAKE THAT TED DZIUBA! Is it any good? One word: yes. Did we miss the point that this isn't about Fibonacci? One word: yes. (Side note: I second raganwald in adding "Is it any good? Yes." to the top of all my READMEs)

Memoizing each member of the Fibonacci sequence makes this algorithm O(n) in memory. For shits and grins I installed Node-fib and asked it for the 1 millionth Fibonacci number. Node ground away (on a single core of my quad core i7 MacBook) for about a minute before running out of memory, crashing the entire VM.

Okay, so mistakenly implementing an algorithm this pathologically bad when you're trying to prove a point to someone who's just trolling you is pretty bad, right? But it gets worse. 

After pointing this out, a member of the Node.js community (post now deleted) suggested I might have an obsolete version of Node with a 1GB heap limit (because Node uses a VM targeted at client-side JavaScript where 1GB heaps are an uncommon use case, but hey, let's throw it on the server!) and that I recompile without the 1GB restriction so that this retarded algorithm can continue eating up all my system RAM. He says this as if this is a good idea instead of, I dunno, using a better algorithm.

Clearly the next logical steps are to store previously computed Fibonacci numbers in MongoDB. Or you could build a system with Socket.IO which caches previously computed Fibonacci numbers client-side using a distributed hash table. Then you'd be well on your way to a multimillion dollar cloud-based Fibonacci number service. Eventually you'll probably want to build in an OpenMP C++ native extension to calculate Fibonacci numbers across multiple CPU cores. That way you'll have a truly roflscale Fibonacci web service.

214 comments:

«Oldest   ‹Older   201 – 214 of 214
Horizon Academy said...

Hi,


Thanks for posting this post and sharing your knowledge through blog. This blog is very helpful or informative.

Udaipur Girl said...

Meet Indian top quality high profile house wife in udaipur best model available in our we gave high-class girls trust me and book your choice.
Goa Girls Mobile Number
Agra Girls Mobile Number
Lucknow girls mobile number
high profile girls

Followerindia said...

Keep sharing your Knowledgeable content through your blog. This blog is very informative to all. Tweet idea for Twitter

Unknown said...

I found your article very useful and informative. Same as I shared informative content about TOGAF in project management. TOGAF is part of Project Management, It is designed to create a career path for professional development and credibility to employers. To learn more about TOGAF, check out this link below and start building your career in project management.
https://www.theknowledgeacademy.com/in/courses/togaf-training/

Anoop said...
This comment has been removed by the author.
Anoop said...

Your content is amazing and more informative. I've like to share my content about PMP, which is more useful to make career in project management

"https://www.theknowledgeacademy.com/in/courses/pmp-training/"

Unknown said...

Check Out this latest Project Management Interview Questions
(Updated 2022) prepared by professionals and experts

Unknown said...
This comment has been removed by the author.
Ahana said...

I really appreciate your blog. It was really helpful for us! Thank you for sharing about NodeJS. I recommend one of the best full stack developer course in Delhi online that offers best training through live projects.

deekshitha said...

Data Science Course in Nasik Best Training Institute Fees, Duration, Certification Online Classes Available There are no limitations to learning courses and one can indeed get multiple certificates provided he she completes the courses and clears the exam. PassedB.Sc. Degree from a honored University as defined by UGC, with at least 45 marks( 40 marks in case of candidates belonging to reserved order) and passed 10 2 examination with Mathematics as a subject. Curated by Hadoop experts, this Big Data Analytics course covers everything you need to gain proficiency in this fielddata science training institute in jaipur

arjun said...

Sustainability of packaging materials available that businesses can adopt to reduce their environmental impact, appeal to environmentally-conscious consumers, and improve their bottom line. Biodegradable plastics, recyclable materials, compostable materials, reusable packaging, minimalist design, renewable materials, innovative materials, local sourcing, reduced packaging size, and transparent labeling are all strategies that can be tried out. By adopting these sustainable packaging materials strategies, businesses can make a positive impact on the environment and society, while also creating a more competitive product. The key to success is to find the right balance between sustainability, product protection, and consumer convenience, while also keeping costs under control. With careful planning and execution, sustainable packaging can be a win-win solution for businesses and the environment.

Unknown said...

Thank you for your useful article, I appreciate your wonderful work, this is a very nice article to read. To know about digital marketing to grow your business as well as increase your knowledge check out this Digital Marketing Training Institute.

Adomas9596 said...

Thanks for sharing beautiful content. I got information from your blog.keep sharing.Thank you quite much for discussing this type of helpful informative article. Will certainly stored and reevaluate your Website.
divorcio indiscutido sin culpa en virginia

nithu said...


I have read your post and i am here to thank you for sharing this post
A Traffic Lawyer Manassas VA is a legal professional who specializes in handling cases related to traffic violations and driving offenses specifically in the Manassas area.

«Oldest ‹Older   201 – 214 of 214   Newer› Newest»