12 ms·
To boldly go where Node man has gone before
- kbd 15y agoSo does Go's net/http module really provide basically exactly the same programming model as Node? If so, why use Node? One thing the author didn't mention is that Go will use multiple cores in this example, whereas Node is single-threaded. Right?
- jgrahamc 15y agoIt's true that Go can use multiple cores, but I was doing these tests in a Ubuntu VM that was restricted to using a single core so that Go's inherent 'multicore' advantage was dialed down. I'll add a note to the post.
- kbd 15y agoThat's a(nother) pretty big real-world advantage for Go then, no? I know the solution for Node is to just run one instance per-core, but it's automatic with Go. I'd love to see a second set of benchmarks of the same code on multiple cores.
- _ak 15y agoYes, Go will do that automatically. Plus, you can write your code in a completely linear fashion, instead of the typical Node.js code with lots of callbacks.
- chc 15y ago> So does Go's net/http module really provide basically exactly the same programming model as Node? If so, why use Node? Well, for starters, Go is much more awkward for working with JSON than JS is (which is to be expected since JSON is literally a subset of JavaScript). More importantly, Go lacks the ridiculously vibrant ecosystem for Web development that Node has — no NPM, no Connect, no Jade, no Stylus, etc.
- paufernandez 15y ago'npm' is covered by the Go tools themselves. You can import packages using their github/bitbucket/code.google.com URL.
- chc 15y agoThat is really interesting. Thanks for pointing that out. Can you specify specific version dependencies like you do with a full-blown package manager?
- paufernandez 15y agoI don't think so, but I could be mistaken. (btw, someone pointed that out as a drawback on another thread.) What I've done sometimes is just clone the repo within a directory that appears in the GOPATH, maybe checkout a specific revision, and that solves the problem.
- BarkMore 15y agoAfter 'go get'ing the package, you can use git/hg/bzr to checkout the specific version you want. From there, all of the Go build tools will work as normal.
- deleted 15y ago[deleted]
- _ak 15y agoSo what exactly do you think is awkward about Go's way of working with JSON? Here's a code example from one of my current projects: type UploadProgress struct { Progress int `json:"progress"` } //... // variable progress is of type Progress if json_data, err := json.Marshal(progress); err == nil { w.Header().Set("Content-Type", "application/json") w.Write(json_data) } json.Unmarshal works the same way. IMHO, not exactly more awkward than using JSON.parse and JSON.stringify.
- chc 15y ago
- samdk 15y agoI don't program in Node very much or in Go at all, but I think there are reasons to use Node that aren't "Node is fast". I think the "Node is fast" mantra was originally really meant as a comparison to Ruby/Python/PHP, not as a comparison to lower-level languages like Go. (I personally wish Node would just abandon speed as a selling point altogether--I think it'd lead to many fewer misunderstandings.) In any case: Node lets you reuse the same code on both the client and server. This can be very valuable if you're writing a JS-heavy webapp. In my experience, the most useful bits of code are the in-code data representation (models in an MVC world). Being able to have identical functionality client- and server-side can save a lot of time. Node also has Socket.IO, which is very useful if you want to use websockets. (Go has a library for it, but it hasn't been updated in a long time.) The vast majority of the Node ecosystem revolves mostly around building interaction- and communication-heavy webapps. I don't know Go or its community at all, but I don't believe they have the same kind of single-minded focus that Node has. If you're trying to do the things Node does well, you will likely benefit from the community support. There are, of course, many downsides to Node as well, but much as I dislike Javascript, I still think it's one of the best choices for writing highly interactive webapps right now.
- jay_kyburz 15y agoYeah, I agree, I'm using node right now to build a big web game and there are huge benefits to being able to run the same code on the sever and client. I wouldn't use node if it wasn't for this because I don't particularly like javascript. All my other games have used Python on the back end and you can really feel how clunky and painful javascript is when you have to switch between the two five times a day.
- kitsune_ 15y agoI doubt it does. net/http looks like a basic http listener? As such, the comparison looks biased to me. Mind you, I don't know much about node.js or Go.
- chc 15y agoIsn't Node's http.Server also a basic HTTP listener? What's the distinction you're trying to draw?
- jff 15y agoWriting http servers with Go really is pretty easy; here's one I wrote while at SC11: https://bitbucket.org/floren/goblog https://bitbucket.org/floren/goblog It's for an older version of Go, and has only been tested on Plan 9, but it shows a static content + blog server, with JSON config files and templated HTML for the blog pages.
- zemo 15y agoNo, the model is completely different. In Node, you pass in a callback to establish what should be done following the return of a longrunning I/O operation. In Go, you use goroutines to manage the concurrency. Basically if you say this: fn() The Go runtime will execute the function in its entirety and wait for a return value. If you say this: go fn() The Go runtime will execute that function in a new goroutine (the return value is discarded) and then immediately proceed to the next line in the current goroutine. So if you have some callDB() function that performs some long-running i/o, in node, you might do something like this to perform that operation without blocking the surrounding code: ... callDB(someparam, otherparam, function (result) { // do something with result here }) ... while in Go, you would do something more like this: ... go func() { result := callDB(someparam, otherparam) // do something with result here }() ... which winds up being more like this func superGreat() { result := callDB(someparam, otherparam) // do something with result here } go superGreat() With the benefit being that if you want superGreat to run concurrently, you use the go keyword, and if you want it to be run in a synchronous style, you just call it normally. The problem with Ruby/Python/etc is that it is awkward to take something that is blocking and make it nonblocking. The problem with node.js is that node.js says "blocking is bad; therefore, never block". Go takes a different approach, in that it makes it obvious to know how to write something so that it does or does not block, so that the developer is free to use whatever I/O paradigm fits the problem at hand. The net/http package allows you to register a handler, which is a function that takes and http request and writes a response. There is a main request loop that, when receiving a request, will create a new goroutine for each request and execute its handler in its own context. So within your handlers, you mostly just write blocking code, because you're already in your own isolated goroutine, and the only thing you'd be blocking is the processing of the current request. If you want to do something in the background, you run it in a new goroutine. goroutines are very cheap, so you can be quite cavalier about their usage. If you want to use a callback-passing style, you can do that in Go (because it has function literals and closures and first-class functions and all that), but that's not idiomatic by a longshot. The absolutist position that node.js takes by saying that "all i/o must be nonblocking" is no better than our previous options of "all i/o is blocking". Sometimes the simplest and most readable solution is simply to block. There is a yin to the yang of blocking.
- shazow 15y agoMy favourite thing about posts like these is the little comparison snippets between a framework I already know (Node) and a language I want to learn (Go). This is very educational, and the benchmarks are the cherry on top. Thank you.
- veemjeem 15y agoI'm sure the reason why users pick node is probably similar reasons why one picks ruby/python -- it's not for the speed. The language of the web is javascript. Until web browsers allow Go as the embeddable language, node will remain useful. I seriously doubt people would pick Go over node because of a micro-benchmark.
- kbd 15y ago> The language of the web is javascript. Until web browsers allow Go as the embeddable language, node will remain useful. Do people really get that much code re-use between client and server that using Javascript on the server provides a large benefit?
- Raynos 15y agoIn a medium sized web application, 50-70% of server-side code can be re-used verbatim between server and client.
- jsprinkles 15y agoWell, whatever "medium sized" means, I work on a high-traffic Web application that reuses 0% of code from the client; in almost everything I've worked on, I've found reuse between client and server nearly impossible, enough that they're almost always segregated repositories. There's an argument for models existing on both sides (which is reiterated below), but I have a hard time seeing models as 50-70% of any application. Just not my experience that this is the case. Though I might be old-fashioned.
- tomjen3 15y agoThe amount of traffic has nothing to do with it. A crud app would properly share relatively little code between the server and the client. A multiplayer game would share much, much more.
- 15y ago
- xxbondsxx 15y agoChoosing Node.JS isn't just a performance / scaling decision for me -- one of the main draws to Node is the huge community and wealth of awesome modules. Socket.io, Now.js, cradle, redis, and dozens of other modules abstract away the tedious work and let me build functionality _really_ fast. Go is really promising but it won't win over the majority of casual developers until there's a community around it. I might as well write a webserver in C if I'm only concerned with performance
- paufernandez 15y agoAs we are commenting here, the community is assembling by the minute... Go allows you to import "github.com/user/package" and will download and compile the code, so creating a new project is even easier than with node, no need to "npm install ..." first. For a taste of what's out there, see: http://go.pkgdoc.org/index http://go.pkgdoc.org/index.
- jlarocco 15y agoThe GitHub thing was neat the first time I saw it, but then I thought about it some more, and I'm not sure it's such a good idea. I couldn't find any way to specify a specific tag or revision, so it's basically always getting the bleeding edge developer build of the library code. That might be nice in some situations, but for getting work done I almost always want the most recent stable release. Am I missing something?
- vito 15y agoI hit this a while ago. The first thing I tried to install failed, because it depended on something that depended on something that moved its repository location (renamed itself from foo.go to foo). It's a convenient hack but nothing more.
- paufernandez 15y agoIf I wanted a specific tag or release, I would just download that revision in the GOPATH directories, and refer to the package directly, instead of through the github URL.
- deleted 15y ago[deleted]
- christkv 15y agoyou know what both platforms are great, I'll use them both thank you.
- bdr 15y agoThe point of Node is that it uses JavaScript, and browsers only speak JavaScript. You have three choices: (1) use Node, (2) duplicate code client- and server-side, or (3) use server-side code that compiles to JS. Right now 3 is the least popular (and 2 is the most) but I think it will end up being the accepted solution in a couple years.
- josephcooney 15y agoOr use a plug-in in the browser that speaks your language of choice.
- mustafaakin 15y agoThe main problem is Go uses every core, while Node does not if you do not use clustering. And with clustering performance scales up linearly, try if you wish.
- adestefan 15y agoAre we now going to have a run of posts that tweak a stupid micro-benchmark?
- david_k 15y agoI can personally vouch for the maturity of Node especially Socket.io. We're building a web-based email client that relies heavily on real-time communication with a Node-based email importer. See http://philterit.com http://philterit.com. I've found Node to be faster and more lightweight than Rails 3, which we initially used for our prototype. With JavaScript being the lingua franca on the client-side, specializing in it as a team has permitted us to master it and offer a far better user experience.
- tinyjoe 15y agoNobody is comparing Node with Assembly. sigh
- moocow01 15y agoIf there is one thing I've learned its to ignore benchmark tests on relatively established technologies. You'll drive yourself into the looney bin if you hop from one thing to the next based on these comparisons that inevitably popup every month or so. If you enjoy programming in JS with Node use it - if you enjoy programming with Go use it. Your enjoyment will far outweigh performance differences that will be minor in 99% of real world cases.
- IsaacSchlueter 15y agoYes, exactly.
- taligent 15y agoAlso it is often external influences that will drive performance improvements e.g. Nginx, Redis, Memcached, Javascript caching etc.
- guscost 15y agoI use Express, because I can make and host a fast web page in 3 minutes, and if I ever come across those fabled unsolvable "scaling problems" I can abandon whatever needs to be rewritten without a second thought. Admittedly this is usually for promotional media and assorted small projects, but it still works great.
- arturadib 15y agoNode is popular not only because it's very fast (yes, it's still pretty darn fast even if there might exist faster alternatives), but because it's JavaScript. That's an enormous advantage for startups as (a) the JavaScript hiring pool is much larger than most other languages (particularly in comparison to nascent ones, like Go), and (b) the ability to write client- and server-side code in the same language means small teams can be far more integrated and collaborative than otherwise (and if the startup has a single coder, the point is even stronger). Assuming these benchmarks are accurate and translate into similar results for more complex applications, I'd gladly pay the 10-20% performance penalty to get the above benefits. Node is fast enough for most startups.
- euroclydon 15y agoI think any person who can write an application in Node could also do it in Go, plus if your having trouble attracting good developers, [edit] Go, not node [/edit] should make the position more attractive.
- deleted 15y ago[deleted]
- virmundi 15y agoI've heard the JavaScript developers are easy to find argument before. However, good JavaScript developers are really hard to find. Especially when you need them to take those skills and run them on the server. There is a big difference between a jQuery hack and a good JavaScript developer.
- antihero 15y agoExactly, I just see the PHP problem repeating itself. Yes, you can get a PHP developer for £7/hour, but are they going to write good code? Unlikely.
- Killswitch 15y agoThis. What attracted me was the fact that Javascript on the server. Javascript for the client, and Javascript for the database (MongoDB) seemed much more appealing than 3 separate languages. (Normally a PHP/MySQL dev)
- m0wfo 15y agoIf you saw a 'hello world' benchmark on shootout.alioth.debian.org you'd laugh and ignore it, so why is there yet another post debating the relative merits of threading models using this dubious criterion?
- blantonl 15y agoI consider myself a pretty conservative developer when it comes to the tools that I use, and mainly stick to the core LAMP stack for my deployments - MySQL, Apache, PHP etc. I've ventured down the path with MongoDB and currently use it for one special use case where the data served is static. But for Node - I've really come to the conclusion that it is a great tool for back end API work, such as serving very short requests, Ajax calls, etc. Couple the ability to develop in Javascript while serving JSON makes Node a really nice place to develop in. Plus, performance is off the charts for my API serving applications. On a dev instance I can serve close to 5500 reqs/sec for one of our APIs where PHP was at about 1000 req/sec. And the difference in memory usage is fantastic. BTW, our production Node deployment is Node, with the memcache, mysql, mysql-pool, cluster, and express modules. I've been thrilled so far.
- sciurus 15y agoOut of curiosity, how were you running PHP?
- euroclydon 15y agoGood God man, your iPhone theme is awful! I tried to pan the screen to view all the results and it navs to the next blog post?!?
- fooooobar 15y agoDid Go ever get it's GC problems fixed?
- exogen 15y ago"Easy to build, fast, scalable." So which part of the test demonstrated that it was hard to build, slow, and/or not scalable? If you're not impressed that JavaScript reached 80% parity with a statically typed, compiled language – well, sorry dude. Anyway, nobody is begging you to be impressed. 99.999% of successful projects are built with technology that is completely unimpressive.
- riobard 15y agoJust want to point out one obvious thing that I found very interesting: that the V8 JavaScript engine which makes Node exist in the first place comes from the same company which makes Go.
- cageface 15y agoGoogle's Robert Griesemer, who worked on the V8 VM, is one of the main contributors to Go.
- ilaksh 15y agoI think that actually the Node example was significantly simpler.. there were no pointers, and no types needed to be specified. Also I can use CoffeeScript with Node. The only way Go is easier to write is if you know Go but don't know Node. I say its easier to learn and use Node/Javascript. I am surprised that Node performed so well.. I would like to see that same Go benchmark with 4 CPUs vs. Node and also vs. Node with a clustered web server and the code to see how much more code that involves for clustering with Node. Also I would like to see some Go code and benchmark that reads a file and/or database in the request and works efficiently. Actually the code for a Node cluster that would use all of the available CPUs for http in CoffeeScript would look like this: cluster = require 'cluster' http = require 'http' numCPUs = require('os').cpus().length if cluster.isMaster cluster.fork() for i in [1..numCPUs] else app = http.createServer() app.on 'request', (req, res) -> res.end "hello world\n" app.listen 8000 Maybe someone could take V8, change it so it compiles (immediately) to static code and uses CoffeeScript, and add a way to optionally specify types (in an unambiguous and readable way) and pointers only when you have to.
- robertpateii 15y agoThis test shows Node handling 100 requests simultaneously just as easily as handling them separately. However Go's memory usage increases by about 5X. So what happens when you reach 1000 simultaneous requests? Which will perform better then? I think this is called "selection bias."
- reissbaker 15y agoNot only that, but Node actually got faster as simultaneous connections increased while keeping a completely stable memory footprint. The benchmark showed the opposite of what the author claimed -- Node scaled effortlessly. Its performance was completely stable and predictable under load, whereas Go's wasn't. Not to knock Go -- it's a beautiful language, and absolutely faster than Javascript for most purposes (and will no doubt increase in stability and speed as time goes on). But this post had some pretty flawed analysis of the benchmark presented.
- supersillyus 15y agoFYI, the original article has been updated with graphs showing reqs/sec, virtual, and real memory for both using 100, 500, and 1000 simultaneous requests. Aside from virtual memory (do we care about that?) it seems to show Go as continuing to perform better.
- robertpateii 15y agoAh, great. That's a good update. Go does end up responding well at 1000 concurrent requests for real memory. But a spike in virtual memory means it's paging all that extra data, right? Doesn't that normally slow things down? what's interesting is Go still maintains a similar requests per second. of course, for a front end guy, Node is still the most exciting thing on the block simply because it is server-side javascript. The fact that it's not much worse than the latest greatest compiled language is pretty exciting to me.
- supersillyus 14y ago
- exogen 15y agoIf you consider something to be fast and scalable (e.g. Go), then something with ~80% of the throughput (e.g. Node in this test) is probably also pretty fucking fast and scalable. Scalability and performance don't walk fine lines like that; there's a reason we talk about orders of magnitude.
- cageface 15y agoStatic typing + no callbacks make Go vs Node a no-brainer, IMO. I'm doing mainly iOS work these days but I've been brainstorming ideas that require a server component just so I can use Go for something.
- lachenmayer 15y agoWhat do you mean by "no callbacks"?
- cageface 15y agoYou can do non-blocking I/O without the nested callbacks you have to use for everything in Node. You just fire off a block to a goroutine and then code sequentially there.
- zoips 15y agoIn the short term: https://github.com/laverdet/node-fibers https://github.com/laverdet/node-fibers In the long term: http://wiki.ecmascript.org/doku.php?id=harmony:generators http://wiki.ecmascript.org/doku.php?id=harmony:generators
- pjscott 15y agoBoth look useful, but aren't exactly well-supported by the library ecosystem. In Go, faux-blocking I/O is standard, and all the libraries support it; it's not something bolted on as a handy afterthought.
- jerf 15y agoNo, and no, respectively. Hacked together libraries to sort of support rewrite it at the wrong level of the stack (the compiler should be doing the continuation transform, anything else will lack performance due to having to jump through a lot more hoops) and merely having generators is not the same thing. And neither solution you propose will give you true preemptive multitasking. Do you know how you fire off a long computation in Go or Erlang? You just do it. No marshalling, no blocking, no dealing with OS processes, no fuss. Neither of those two things are new ideas. Python has had generators forever. If it solved the underlying problems, Go would never have been written.
- jorangreef 15y agoAny language faster than Javascript would be fine if it worked in the browser.
- tluyben2 15y agoI pick Node because it's easy to move code from backend to frontend and back. I find this very comfortable while creating and testing software.