GoLang   

 

VS

NodeJS   

 

Why Go over Node?

Adaptibility measure

  • Performance perspective
  • Development perspective
  • Deployment perspective

Performance

Comparing http servers

Specifications

Memory 7.7 GiB

Processor Intel® Core™ i5-6500 CPU @ 3.20GHz × 4

OS type 64-bit

Disk 483.8 GB

v8.11.3
go1.9

100000 Requests
100 Concurrent

 

Apache Benchmark

package main

import (
    "fmt"
    "log"
    "net/http"
)


func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "GOLANG!")
} 


func main() {
    http.Handle("/", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

GoLang



const http = require('http');

function requestHandler = (request, response) => {
    response.end('NODEJS!')
}

const server = http.createServer(requestHandler);

server.listen(8081, (err) => {
    if(err) {
        console.log('something bad happened', err)
    }
})

NodeJS

 

17154

Requests/sec

30568

Requests/sec

30568/17154 = 1.78197505 times better to be precise

 

Lets be real and install some packages, right?

After all, we're developers.

npm install express -S​
go get -u gorilla/mux 
const express = require('express');
const app = express();

app.get('/hello', (request, response) => {
    response.send('NODEJS!')
});

app.listen(8081);

NodeJS with ExpressJS

package main

import (
    "fmt"
    "log"
    "net/http"
    "gorilla/mux"
)

func HelloHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "GOLANG!")
}

func main() {
 
  r := mux.NewRouter()
  r.HandleFunc("/hello", HelloHandler)
  log.Fatal(http.ListenAndServe(":8080", r));

}

GoLang with Mux

10290

Requests/sec

30440

Requests/sec

30440/10290 = 2.95821186 times better to be precise

 

Lets do some work

Hashing

const http = require('http');
const crypto = require('crypto');


const requestHandler = (request, response) => {


    for(let i =0 ; i <= 100; i++) {
        crypto.createHash('sha256')
        .update("123456789")
        .digest("hex");
    }

    response.end("");
}

const server = http.createServer(requestHandler)

server.listen(8081, (err) => {
    if (err) {
        return console.log('something bad happened', err);
    }
});

NodeJS

n=100
package main

import (
	"crypto/sha256"
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
	for i := 0; i <= 1000; i++ {
		h := sha256.New()
		h.Write([]byte("123456789"))
		fmt.Sprintf("%x", h.Sum(nil))

	}
	fmt.Fprintf(w, "")
}

func main() {

	r := mux.NewRouter()
	r.HandleFunc("/hello", helloHandler)
	log.Fatal(http.ListenAndServe(":8080", r))
}

GoLang

n=1000

GoLang With Mux

Raw NodeJS

4329.21667

Requests/sec

31206.0567

Requests/sec

31206.0567/31206.0567 = 7.20824553 times better to be precise

 

{
    "apps": [{
        "name": "node",
        "script": "./app.js",
        "exec_mode": "cluster",
        "instances": 4
    }]
}

PM2 Cluster Mode

7213.67333

Requests/sec

31206.0567

Requests/sec

31206.0567/7213.67333 = 4.32595923 times better to be precise

 

Demystifying NodeJS and the Event Loop

NodeJS is single threaded, except its not.

NodeJS uses multi threading.

const crypto = require('crypto')

const NUM_REQ = 2;

for (let i = 0 ;i < NUM_REQ; i++) {

     crypto.pbkdf2('secret', 'salt', 10000, 512, 'sha512')
}

Sync crypto code.

https://www.youtube.com/watch?v=zphcsoSJMvM
The Node.js Event Loop: Not So Single Threaded

 

const crypto = require('crypto')

const NUM_REQ = 2;

for (let i = 0 ;i < NUM_REQ; i++) {
     crypto.pbkdf2('secret', 'salt', 10000, 512, 'sha512', () => {})
}

https://www.youtube.com/watch?v=zphcsoSJMvM
The Node.js Event Loop: Not So Single Threaded

https://www.youtube.com/watch?v=zphcsoSJMvM
The Node.js Event Loop: Not So Single Threaded

NodeJS uses a pre-allocated set of threads call the Thread pool (Default=4)

https://www.youtube.com/watch?v=zphcsoSJMvM
The Node.js Event Loop: Not So Single Threaded

How does GoLang do it?

Goroutines

What are Goroutines?

Runtime scheduler runs goroutines by mapping them onto operating system threads. Goroutines are lightweight version of threads, with very low cost of starting up.

https://www.youtube.com/watch?v=Yx6FBsGNOp4&t=215s
dotGO 2017 - JBD - Go's work stealing scheduler

Developer Perspective

Amazing Community!

It's Javascript!

Rapid Prototyping


const app = require('express')();
app.post('/login', (req, res) => {
      
    authService.login(req.body).then(
     authToken => res.json({ Authorization: authToken })
    );  
      
})

app.listen(3000);

If you really want it that way.

Whats so cool about Golang?

It's really really fast.

really fast.

Go concurrency model

Lightweight, concurrent function execution. You can spawn tons of these if needed and the Go runtime multiplexes them onto the configured number of CPUs/Threads as needed.

I left lorem ipsum here for fun.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.

For concurrency, all you have to do is this.

f()        go f()

Its strictly typed.

It has a concept of implicit interfacing which is really awesome.
 

No whining for typescript anymore!

It has a Standard Library

sql, driver, strconv, fmt, log, net, net/http, json 

you don't have to dig down the right package to get your job done.

It has ITS OWN LINT!

You don't have to worry about your lint versions or lintrc's anymore.

Channels

Complete control over your code.

Unlike the third promise in 

Promise.all([p1, p2, p3])

Your whole application can be built into a single executable file.

Let it be Windows, Linux, or MacOS

Straight forward testing

No need for chai, mocha and weird named stuff.

 

Go has its own test suite.

Anonymous functions and Closures


func intSeq() func() int {
    i := 0
    return func() int {
        i++
        return i
    }
}

Deployment perspective

Your entire application as a single binary file.

Static Linking

Just push your binary over to the server and bash it!

Your server doesn't need Golang to run Golang on it. Forget about version issues.

Package Management through dep

Cloud Service Support for Go

 

Go Programming Language is a product baked at Google by the three greatest minds in the programming world. Considering the credentials Go showcases, it is not a shocker that many companies other than Google have adopted Go.

Facebook

Twitter

YouTube

Apple

Dropbox

Docker

Soundcloud

Mozilla Firefox

Medium

The New York Times

Github

GOV.UK

UBER

Why not us?

Questions?

Please don't ask the hard ones

Made with Slides.com