Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Last active May 26, 2017 18:46
Show Gist options
  • Save hajimehoshi/5ed631114901acc7a7ce019449c6bf36 to your computer and use it in GitHub Desktop.
Save hajimehoshi/5ed631114901acc7a7ce019449c6bf36 to your computer and use it in GitHub Desktop.
GopherJS's defer performance
package test
import (
"testing"
)
var i = 0
func begin() {
i++
}
func end() {
i--
}
func fooNormal() {
begin()
end()
}
func fooDefer() {
begin()
defer end()
}
func BenchmarkNormal(b *testing.B) {
for i := 0; i < b.N; i++ {
fooNormal()
}
}
func BenchmarkDefer(b *testing.B) {
for i := 0; i < b.N; i++ {
fooDefer()
}
}
$ gopherjs version
GopherJS 1.8-1
$ gopherjs test --bench="." github.com/hajimehoshi/test
gopherjs: Source maps disabled. Install source-map-support module for nice stack traces. See https://github.com/gopherjs/gopherjs#gopherjs-run-gopherjs-test.
BenchmarkNormal 300000000 5.26 ns/op
BenchmarkDefer 10000000 137 ns/op
PASS
fatal error: all goroutines are asleep - deadlock!
FAIL github.com/hajimehoshi/test 4.047s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment