Skip to content
This repository was archived by the owner on Feb 9, 2024. It is now read-only.

Conversation

@groenlid
Copy link

@groenlid groenlid commented Aug 16, 2020

This should fix issue #139 where ASSET_MANIFEST is parsed on each call to getAssetFromKV.

I'm currently using this library for a medium to large gatsby project (734MB over 53232 files) and this change would improve our performance quite a bit.

@SukkaW
Copy link
Contributor

SukkaW commented Aug 16, 2020

LGTM!

Could you provide any benchmark result or real time performance data for the PR?

For example:

/* index.js */
// ...
let BEGIN_TIME, END_TIME;

addEventListener('fetch', event => {
    BEGIN_TIME = Date.now();

    event.respondWith(handleEvent(event));
})

async function handleEvent(event) {
  // ...
  const response = await getAssetFromKV(event, kvOptions);

  END_TIME = Date.now();

  if (typeof BEGIN_TIME === 'number' && typeof END_TIME === 'number') {
    response.headers.append('x-kv-time', `resp:${END_TIME - BEGIN_TIME}ms`);
  }

  return response
}

And see if x-kv-time being different.

@groenlid
Copy link
Author

groenlid commented Aug 17, 2020

Sure! I've ran the following code with a couple of different iteration-counts. Hope that's ok.

import fetch from 'node-fetch'

const iterations = 1000
const url = '' // Url to single resource

let sum = 0

const runTest = async () => {
    for(let k = 0; k < iterations ; k++) {
        const response = await fetch(url)

        if(!response.ok) throw new Error('Response was not ok!')
        const timeHeaders = response.headers.get('x-kv-time');
        const first = Array.isArray(timeHeaders) ? timeHeaders[0] : timeHeaders
        const number = parseInt(first.replace('ms', ''), 10)
        sum += number
    }
}

runTest().then(() => {
    console.log('Done. Mean kv response time is ' + sum / iterations)
})

Before change: (Edit: had to run this again. Forgot to remove a yarn link before publishing)
100 iterations: Done. Mean kv response time is 16.61
1000 iterations: Done. Mean kv response time is 17.798

After change
100 iterations: Done. Mean kv response time is 6.62
1000 iterations: Done. Mean kv response time is 7.296

Copy link

@harrishancock harrishancock left a comment

Choose a reason for hiding this comment

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

Makes sense to me. Thanks, @groenlid!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants