Skip to content

Instantly share code, notes, and snippets.

@ehfeng
Created June 27, 2023 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehfeng/d26ef78b0ef869d6feca7abced948fb5 to your computer and use it in GitHub Desktop.
Save ehfeng/d26ef78b0ef869d6feca7abced948fb5 to your computer and use it in GitHub Desktop.
Performance cost of starting isolates in parallel
package main
import (
"fmt"
"runtime"
"sync"
"time"
v8 "rogchap.com/v8go"
)
func startIsolatesInParallel(n int) {
start := time.Now()
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
isolate := v8.NewIsolate()
defer isolate.Dispose()
v8.NewContext(isolate)
wg.Done()
}()
}
wg.Wait()
fmt.Println(n, ":", time.Since(start))
}
func main() {
var start time.Time
start = time.Now()
isolate := v8.NewIsolate()
v8.NewContext(isolate)
isolate.Dispose()
fmt.Println("Warmup:", time.Since(start))
startIsolatesInParallel(1)
startIsolatesInParallel(1)
startIsolatesInParallel(2)
startIsolatesInParallel(2)
startIsolatesInParallel(4)
startIsolatesInParallel(4)
startIsolatesInParallel(10)
}
// Warmup: 8.218875ms
// 1 : 1.521208ms
// 1 : 1.216208ms
// 2 : 1.378416ms
// 2 : 1.241666ms
// 4 : 1.676334ms
// 4 : 2.10425ms
// 10 : 5.307583ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment