opral / inlang-paraglide-js

Tree-shakable i18n library build on the inlang ecosystem.
https://inlang.com/m/gerre34r/library-inlang-paraglideJs
23 stars 0 forks source link

Use Arrays instead of Language-Records for index-functions #68

Open LorisSigrist opened 2 months ago

LorisSigrist commented 2 months ago

Context

This is an optimization that was pointed out to me by a dev from Galaxus.ch

We currently do a record-lookup for the language-specific message function when evaluating index-message functions

export const my_message = () => {
  return {
    en: en.my_message,
    de: de.my_message
  }[languageTag()]()
}

Proposal

If we instead had a canonical order for the languages we could do an array-lookup instead. This would save 3 chars per language per message.

export const my_message = () => [en.my_message, de.my_message][languageTagIdx()]()

If we have 5 languages on average, that's 15bytes per message, so on a page with 50 messages that would be over half a kb on a normal page. That's over 20%.