queen-raae / gatsby-remark-oembed

A GatsbyJS Plugin that transforms oembed links into its corresponding embed code.
https://gatsby-remark-oembed.netlify.com/
MIT License
162 stars 25 forks source link

Improve performance in `getProviderEndpointForLinkUrl` #140

Open raae opened 3 years ago

raae commented 3 years ago

The code in this function has three nested loops, so it's O(n3) (cubic time). We can probably improve upon this.

_Originally posted by @nickytonline in https://github.com/raae/gatsby-remark-oembed/pull/132#discussion_r519533630_

raae commented 3 years ago

We do have a step where the provider list is prepped for use, maybe change the structure of it so its easier to match url with schema.

Ie. going from this:

[
  {
    provider_name: "Twitter",
    provider_url: "http://www.twitter.com/",
    endpoints: [
      {
        schemes: [
          "https://twitter.com/*/status/*",
          "https://*.twitter.com/*/status/*"
        ],
        url: "https://publish.twitter.com/oembed"
      }
    ]
  }
]

to this:

[
  {
    scheme: "https://twitter.com/*/status/*",
    provider_name: "Twitter",
    provider_url: "http://www.twitter.com/",
    endpoint: "https://publish.twitter.com/oembed"
  },
  {
    scheme: "https://*.twitter.com/*/status/*",
    provider_name: "Twitter",
    provider_url: "http://www.twitter.com/",
    endpoint: "https://publish.twitter.com/oembed"
  }
]

Then we could loop through once per oEmbed link, and can break out of the loop as soon as we find it.