Skip to content

Instantly share code, notes, and snippets.

@galkin
Last active June 20, 2024 20:05
Show Gist options
  • Save galkin/0bed2a054d4449f72b0ce8f15ef2693e to your computer and use it in GitHub Desktop.
Save galkin/0bed2a054d4449f72b0ce8f15ef2693e to your computer and use it in GitHub Desktop.
const cachedVar = process.env.MY_ENV_VAR;
const cacheObject = structuredClone(process.env);
const cached = process.env.MY_ENV_VAR;
const loopsCount = 1_000_000;
function getEnvVarDirectly() {
return process.env.MY_ENV_VAR;
}
function getEnvVarFromCache() {
return cachedVar;
}
function getEnvVarFromCachedObject() {
return cacheObject.MY_ENV_VAR;
}
console.time('Cached access from object');
for (let i = 0; i < loopsCount; i++) {
getEnvVarFromCachedObject();
}
console.timeEnd('Cached access from object');
console.time('Cached access from variable');
for (let i = 0; i < loopsCount; i++) {
getEnvVarFromCache();
}
console.timeEnd('Cached access from variable');
console.time('Direct access');
for (let i = 0; i < loopsCount; i++) {
getEnvVarDirectly();
}
console.timeEnd('Direct access');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment