Optimizing Your Code

You may want to optimize your code to avoid reaching the limits for data storage, data requests, or backend requests. You can also upgrade your plan to receive more resources. Read about premium plans to understand the limitations that exist and which plans increase which limits.

Storage

You can take the following steps to reduce the amount of storage your site uses:

  • Delete data you don’t need: Make sure your collections contain only data that you need for your site.
  • Use an external database: The storage quota doesn't apply to external databases. If none of the other options work for you, store your data in an external database and integrate it with your site. You can still access this data using Velo's Wix Data API.

Data requests

You can optimize your data requests by reducing the number of requests you make or by decreasing the requests' processing time.

Amount of requests

You can take the following steps to reduce the number of data requests in your code:

  • Use the bulk functions: If you are inserting or updating multiple items, use the bulkInsert() and bulkUpdate() functions instead of writing each item individually. These functions count as 1 request toward the quota.
  • Make requests only when you need the data: Don’t make requests for data you don’t use. For example, if you store extra product data in a separate collection, query this collection only when you need the data.

Processing time

You can take the following steps to reduce the processing time of the data requests in your code:

  • Request only data you need: Avoid fetching more items from your collection than you need at once. Also avoid including reference items in your queries if you don’t need their data.
  • Use indexes: Querying with an index is faster than querying items directly. All _id and _createdDate fields are indexed by default.
  • Process data in chunks: Timeouts can happen when you process too much data with one request. Breaking your request into smaller pieces means that each individual request will complete before the time limit. If you are running a slow process or loading a large amount of data, use pagination or process your data in chunks. Don't create too many requests, as there is also a quota on requests per minute.
  • Use an external database: The request timeout and processing time quotas don't apply to external databases. If none of the other options works for you, store your data in an external database and integrate it with your site. You can still access this data using Velo and the Wix Data API.

Backend requests

You can optimize your backend requests by reducing the number of requests you make or by decreasing the requests' processing time.

Amount of requests

You can take the following steps to reduce the number of backend requests in your code:

  • Batch your requests: Batch multiple backend requests into one. Make sure not to batch time-consuming requests together as there's also a time limit for backend requests.

    See an example

    Instead of calling a series of backend functions from your frontend code, like this:

    Copy

    You can batch the requests together in the backend and make one call to the batch function from the frontend, like this:
    Frontend code

    Copy

    Backend code

    Copy
  • Avoid repetition: Avoid repeating large database requests or complex calculations. If you can, cache the result or aggregate database queries.

Processing time

You can take the following steps to reduce the processing time of the backend requests in your code:

  • Identify parts of your code that are running for too long: Identify the parts of your code that are taking too long to run, and optimize them to improve performance. You can do this by including console logs before and after the code you want to test, and then checking to see how long it takes to run.

    See an example

    To test your code’s running time using console logs, do the following:

    1. In your backend code, add console.time() before calling a function and console.timeEnd() after the call.

      For example:

      Copy
    2. Open Wix Logs log.

    3. Use Functional Testing to trigger your backend code.

    4. Check the log to see how long it took your function to run.

    The results are displayed in your Wix Logs window:

  • Check your 3rd-party APIs: If you are using 3rd-party APIs on your site, make sure they are not too slow. For example, if you notice an API call to a 3rd-party is taking a few seconds to run, break it into smaller calls or try upgrading your 3rd-party account to improve performance.

  • Process data in chunks: If you are running a slow process or loading a large amount of data, use pagination or process your data in chunks. Don't create too many requests, as there is also a quota on requests per minute.

  • Use indexes: Querying with an index is faster than querying items directly. All _id and _createdDate fields are indexed by default.

Did this help?