Skip to content

Instantly share code, notes, and snippets.

@spamshaker
Created June 22, 2024 04:22
Show Gist options
  • Save spamshaker/7c5a64c9544e04dfc9b0906422b8d706 to your computer and use it in GitHub Desktop.
Save spamshaker/7c5a64c9544e04dfc9b0906422b8d706 to your computer and use it in GitHub Desktop.
Performs quick bench for running code in vm against main process
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