Last active
December 10, 2019 16:58
-
-
Save sporto/6258909 to your computer and use it in GitHub Desktop.
Concurrency: JavaScript vs Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"time" | |
) | |
var baseUrl string = "http://localhost:8080" | |
func exitOnError(err error) { | |
if err != nil { | |
fmt.Println("%s", err) | |
os.Exit(1) | |
} | |
} | |
func getValue(url string, c chan string) error { | |
fmt.Println("Getting " + url) | |
res, err := http.Get(baseUrl + url) | |
exitOnError(err) | |
defer res.Body.Close() | |
body, err := ioutil.ReadAll(res.Body) | |
exitOnError(err) | |
s := string(body) | |
fmt.Println("Result => ", s) | |
c <- s | |
return nil | |
} | |
func processConcat(x string, y string) (string, error) { | |
res, err := http.Get(baseUrl + "/concat?x=" + x + "&y=" + y) | |
exitOnError(err) | |
defer res.Body.Close() | |
body, err := ioutil.ReadAll(res.Body) | |
exitOnError(err) | |
s := string(body) | |
fmt.Println("Result from concat => ", s) | |
return s, nil | |
} | |
func main() { | |
startTime := time.Now() | |
var cx chan string = make(chan string) | |
var cy chan string = make(chan string) | |
go getValue("/x", cx) | |
go getValue("/y", cy) | |
x := <-cx | |
y := <-cy | |
processConcat(x, y) | |
endTime := time.Now() | |
fmt.Println("ElapsedTime in seconds:", endTime.Sub(startTime)) | |
os.Exit(0) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var Q = require('q'); | |
var startTime = process.hrtime(); | |
var defX = Q.defer(); | |
var defY = Q.defer(); | |
var oneAndTwo = Q.all([defX.promise, defY.promise]).then(processConcat); | |
var baseUrl = "http://localhost:8080"; | |
request(baseUrl + '/x', makeValueHandler(defX)); | |
request(baseUrl + '/y', makeValueHandler(defY)); | |
function makeValueHandler(def) { | |
return function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
console.log('Result from', body); | |
def.resolve(body); | |
} else { | |
console.log(error); | |
def.reject(error); | |
} | |
} | |
} | |
function processConcat(res) { | |
//console.log('combined results', res); | |
var url = baseUrl + "/concat?x=" + res[0] + "&y=" + res[1]; | |
//console.log(url); | |
request(url, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
console.log('Result from concat => ', body); | |
} else { | |
console.log("Error"); | |
} | |
printElapsedTime(); | |
}); | |
} | |
function printElapsedTime() { | |
var precision = 3; // 3 decimal places | |
var elapsed = process.hrtime(startTime); | |
console.log('ElapsedTime in seconds: %d.%d', elapsed[0], elapsed[1]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var url = require('url'); | |
var requestListener = function (request, response) { | |
var url_parts = url.parse(request.url, true); | |
var path = url_parts.pathname; | |
var query = url_parts.query; | |
switch(path) { | |
case '/concat': | |
var x = url_parts.query.x; | |
var y = url_parts.query.y; | |
respondWith(x + " " + y, 1000); | |
break; | |
case '/x': | |
respondWith("Hello", 2500); | |
break; | |
case '/y': | |
respondWith("World", 1400); | |
break; | |
default: | |
response.writeHead(404); | |
response.end(); | |
} | |
function respondWith(body, delay) { | |
setTimeout(function () { | |
response.writeHead(200); | |
response.end(body); | |
}, delay); | |
} | |
} | |
var server = http.createServer(requestListener); | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment