Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: load context from global variable #87

Merged
merged 1 commit into from
Nov 2, 2023

Conversation

eduardoboucas
Copy link
Member

Which problem is this pull request solving?

Currently, the client is loading the context from an environment variable called NETLIFY_BLOBS_CONTEXT, so both serverless functions and edge functions need to set the updated context on each request, by setting process.env.NETLIFY_BLOBS_CONTEXT in Node.js and by calling Deno.env.set() in Deno.

I wanted to see if there was any performance penalty in calling Deno.env.set() versus just setting a global variable, so I ran this benchmark and ran it a few times:

Deno.bench("Environment variable", { group: "timing", baseline: true }, () => {
  for (let i = 0; i < 50_000; i++) {
    Deno.env.set("SOME_VAR", `foo${i}`);
  }
});

Deno.bench("Global variable", { group: "timing" }, () => {
  for (let i = 0; i < 50_000; i++) {
    // @ts-expect-error No types for global variable
    globalThis.someVar = `foo${i}`;
  }
});

These are the results I'm seeing:

cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
runtime: deno 1.35.1 (x86_64-apple-darwin)

file:///Users/eduardoboucas/Sites/tests/deno-bench/index.ts
benchmark                 time (avg)             (min … max)       p75       p99      p995
------------------------------------------------------------ -----------------------------
Environment variable      43.42 ms/iter     (39.8 ms … 52.1 ms)  43.95 ms   52.1 ms   52.1 ms
Global variable               2 ms/iter     (1.68 ms … 3.34 ms)   2.13 ms      3 ms   3.09 ms

summary
  Environment variable
   21.72x slower than Global variable

I'm consistently seeing the environment variable set being ~20x slower than the global variable equivalent. In an environment where every millisecond counts, I think we should try to optimise this as best as we can.

So in this PR I'm making it possible for the client to read the context from a netlifyBlobsContext environment variable before trying to read it from an environment variable.

@eduardoboucas eduardoboucas requested a review from a team as a code owner November 2, 2023 09:50
Copy link

netlify bot commented Nov 2, 2023

Deploy Preview for blobs-js ready!

Name Link
🔨 Latest commit 5167310
🔍 Latest deploy log https://app.netlify.com/sites/blobs-js/deploys/654370fbc25f7600085c4f43
😎 Deploy Preview https://deploy-preview-87--blobs-js.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Nov 2, 2023
@eduardoboucas eduardoboucas merged commit cbb5c3d into main Nov 2, 2023
15 checks passed
@eduardoboucas eduardoboucas deleted the feat/context-global-variable branch November 2, 2023 10:12
Copy link
Contributor

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants