Created
June 22, 2024 04:22
-
-
Save spamshaker/7c5a64c9544e04dfc9b0906422b8d706 to your computer and use it in GitHub Desktop.
Performs quick bench for running code in vm against main process
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
import { createContext, runInContext} from 'node:vm'; | |
import { performance } from 'node:perf_hooks'; | |
const fibonacci = n => n <= 2 ? n - 1 : fibonacci(n - 1) + fibonacci(n - 2); | |
const context = createContext({ fibonacci }); | |
let time; | |
let result; | |
time = performance.now(); | |
result = fibonacci(45); | |
console.log('fib current process', result, 'time:', performance.now() - time + 'ms'); | |
time = performance.now(); | |
result = runInContext('fibonacci(45)', context); | |
console.log('fib from vm process', result, 'time:', performance.now() - time + 'ms'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment