You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/blog/2015-09-27-surprises-in-gopherjs-performance.mdown
+10-7Lines changed: 10 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
---
2
2
layout: post
3
-
title: "Surprises in GohperJS Performance"
3
+
title: "Surprises in GopherJS Performance"
4
4
date: 2015-09-27
5
5
author: Dmitri Shuralyov
6
6
---
7
7
8
-
The GohperJS project first caught my attention about 2 year ago, back when few parts of the Go spec were implemented.
8
+
The GopherJS project first caught my attention about 2 year ago, back when few parts of the Go spec were implemented.
9
9
However, I noticed the incredible pace at which Richard was working, making multiple sophisticated commits per day, as well as fixing reported compiler issues within hours.
10
10
A few months later, I decided to download it and give it try on a relatively [large pure Go package](https://godoc.org/github.com/shurcooL/markdownfmt/markdown?import-graph&hide=1) for formatting Markdown, and I was quite shocked when it... [simply worked](https://github.com/shurcooL/atom-markdown-format/commit/6b5f21c4457309f8eba3a78b82e0c9a458ff13b4).
11
11
@@ -65,7 +65,7 @@ total time taken is: 8.358498915s
65
65
Then I got curious how long it would take if compiled to JavaScript via GopherJS.
66
66
67
67
I realized that this is a very tight loop, so any overhead incurred by the conversion of Go to JavaScript would be multiplied and be very visible.
68
-
Still, I was curious, so fired up GohperJS and ran the same program by compiling it to JavaScript and running it with node:
68
+
Still, I was curious, so fired up GopherJS and ran the same program by compiling it to JavaScript and running it with node:
69
69
70
70
```bash
71
71
$ gopherjs run main.go
@@ -91,14 +91,16 @@ And got the same time in Chrome browser (stable channel).
91
91
92
92
The calculated value of pi was the same, and after adding some debugging statements I was sure the calculation was indeed correct, and iterations were not being skipped.
93
93
94
-
But how could it be that taking this Go program and compiling it to JavaScript and executing that would be 4 times faster? I had to get to the bottom of it.
94
+
But how could it be that taking this Go program and compiling it to JavaScript and executing that would be 4 times faster?
95
+
I had to get to the bottom of it.
95
96
96
-
The first thing I needed to ensure, was the same code being run in both cases? The entire code is plain Go, with the exception of `math.Pow`.
97
+
The first thing I needed to ensure, was the same code being run in both cases?
98
+
The entire code is plain Go, with the exception of `math.Pow`.
97
99
So I looked at how [Go implements it](http://gotools.org/math#Pow).
98
100
Pretty straightforward Go code.
99
101
Now I knew GopherJS uses some JavaScript native APIs to implement parts of the standard library, so I checked how [it implemented `math.Pow`](https://github.com/gopherjs/gopherjs/blob/master/compiler/natives/math/math.go#L157).
100
102
Aha! It's not the same code after all.
101
-
GohperJS implements it by using [JavaScript's `Math` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math), so it translates to:
103
+
GopherJS implements it by using [JavaScript's `Math` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math), so it translates to:
102
104
103
105
```JavaScript
104
106
Math.pow(x, y)
@@ -233,7 +235,8 @@ user 0m11.377s
233
235
sys 0m0.006s
234
236
```
235
237
236
-
11.3 seconds? Slower? Ah, of course, I was too used to `go` build tool that uses optimization by default, and forgot that C compilers don't do that.
238
+
11.3 seconds? Slower?
239
+
Ah, of course, I was too used to `go` build tool that uses optimization by default, and forgot that C compilers don't do that.
0 commit comments