fredericbarthelet / typebridge

Typescript toolbox for AWS EventBridge
76 stars 10 forks source link

Update imports syntax in v0.x to reduce cold start duration #16

Open Eikix opened 2 years ago

Eikix commented 2 years ago

Context: aws-sdk v2 uses cjs and thus does not tree shake. This means that the following syntax: import type { EventBridge } from 'aws-sdk'; will result in importing aws-sdk package in its entirety, rather than just EventBridge type.

Note that this is fixed in aws-sdk v3.

Impact: Increasing cold starts for lambdas (up to +1.3s on cold start duration). How? For instance, a lambda uses this package (v0.7) when sending an event to EventBridge.

Proposed fix: In versions < 1, adopt the following import syntax: import type EventBridge from 'aws-sdk/EventBridge';

Thanks:)

fredericbarthelet commented 2 years ago

Hi @Eikix and thanks for the proposal :)

I believe import type statements are stripped altogether at compilation by Typescript, and are therefore not used during bundling phase. You can learn more about this in https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html. That's why aws-sdk is not part of this library at all, and solely used as a dev dependency in order to run tests on the actual implementation.

However, you should indeed in your lambda implementation using typebridge make sure to instantiate EventBridge client using import EventBridge from 'aws-sdk/EventBridge';

Did you see some problem in bundling even implementing SDK client import as described above ?

Eikix commented 2 years ago

Hi @fredericbarthelet ! Thank you for your time. You're absolutely right, thanks for pointing this out:).

PS: I'm sure you meant import EventBridge from 'aws-sdk/clients/eventbridge';