All Projects → microsoft → React Native Lazy Index

microsoft / React Native Lazy Index

Licence: mit
RAM bundle friendly, bundle-time generated `index.js`

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Native Lazy Index

Lozad.js
🔥 Highly performant, light ~1kb and configurable lazy loader in pure JS with no dependencies for responsive images, iframes and more
Stars: ✭ 6,932 (+4747.55%)
Mutual labels:  performance, lazy-loading
React Cool Inview
😎 🖥️ React hook to monitor an element enters or leaves the viewport (or another element).
Stars: ✭ 830 (+480.42%)
Mutual labels:  performance, lazy-loading
React Intersection Observer
React implementation of the Intersection Observer API to tell you when an element enters or leaves the viewport.
Stars: ✭ 2,689 (+1780.42%)
Mutual labels:  performance, lazy-loading
Yall.js
A fast, flexible, and small SEO-friendly lazy loader.
Stars: ✭ 1,163 (+713.29%)
Mutual labels:  performance, lazy-loading
Scouter
Scouter is an open source APM (Application Performance Management) tool.
Stars: ✭ 1,792 (+1153.15%)
Mutual labels:  performance
Sltbench
C++ benchmark tool. Practical, stable and fast performance testing framework.
Stars: ✭ 137 (-4.2%)
Mutual labels:  performance
Laravel Responsecache
Speed up a Laravel app by caching the entire response
Stars: ✭ 1,874 (+1210.49%)
Mutual labels:  performance
Micro Memoize
A tiny, crazy fast memoization library for the 95% use-case
Stars: ✭ 135 (-5.59%)
Mutual labels:  performance
Lithoxyl
Application instrumentation and logging, with a geological bent.
Stars: ✭ 141 (-1.4%)
Mutual labels:  performance
Js Search
JS Search is an efficient, client-side search library for JavaScript and JSON objects
Stars: ✭ 1,920 (+1242.66%)
Mutual labels:  performance
Image Optimize Command
Easily optimize images using WP CLI
Stars: ✭ 138 (-3.5%)
Mutual labels:  performance
Fragment Cache
WordPress plugin for partial and async caching.
Stars: ✭ 135 (-5.59%)
Mutual labels:  performance
Fast React Server
[DEPRECATED] Use last versions of React and Node.js for better performance
Stars: ✭ 139 (-2.8%)
Mutual labels:  performance
Lighthouse Monitor
Investigate performance over your whole company with lighthouse
Stars: ✭ 136 (-4.9%)
Mutual labels:  performance
Forge
High Performance Visualization
Stars: ✭ 140 (-2.1%)
Mutual labels:  performance
Perf Tools
⏱→ 🚀A set of tools for improving performance your application (balancer, performance, PerfKeeper, LazyPromise).
Stars: ✭ 135 (-5.59%)
Mutual labels:  performance
Foxify
The fast, easy to use & typescript ready web framework for Node.js
Stars: ✭ 138 (-3.5%)
Mutual labels:  performance
Ltecleanerfoss
The last Android cleaner you'll ever need!
Stars: ✭ 141 (-1.4%)
Mutual labels:  performance
Estimo
Evaluates how long the browser will execute your javascript code.
Stars: ✭ 138 (-3.5%)
Mutual labels:  performance
Wperf
A simple HTTP load testing utility with detailed performance metrics.
Stars: ✭ 138 (-3.5%)
Mutual labels:  performance

react-native-lazy-index

npm version

react-native-lazy-index is a RAM bundle friendly, bundle-time generated index.js. Improve your app startup time by only loading features you'll use on demand.

For information on RAM bundles and inline requires, see React Native Performance.

If you use Haul, also take a look at their documentation.

Installation

npm install --save react-native-lazy-index

Usage

react-native-lazy-index uses babel-plugin-codegen, so you'll need to configure Babel to include it. The recommended way is to add it to your .babelrc:

{
  "plugins": ["codegen"]
}

In your package.json, add a section called "experiences" with the features that should be lazy loaded. In the example below, we've listed four packages:

{
  "name": "my-awesome-app",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "@awesome-app/some-feature": "*",
    "@awesome-app/another-feature": "*",
    "@awesome-app/yet-another-feature": "*",
    "@awesome-app/final-feature": "*",
    "react": "16.13.1",
    "react-native": "0.63.4",
    "react-native-lazy-index": "^2.0.0"
  },
  "experiences": [
    "@awesome-app/some-feature",
    "@awesome-app/another-feature",
    "@awesome-app/yet-another-feature",
    "@awesome-app/final-feature"
  ]
}

That's it!

Why

With a naive index.js, all features will be loaded when your app starts and React Native is initialized for the first time.

import "@awesome-app/some-feature";
import "@awesome-app/another-feature";
import "@awesome-app/yet-another-feature";
import "@awesome-app/final-feature";

By loading features on demand, we can improve app startup time.

With react-native-lazy-index, we no longer load all features up front. Instead, index.js wraps calls to AppRegistry.registerComponent and BatchedBridge.registerCallableModule, deferring the import of a feature until it is used. Features that are never used, are never loaded.

When you import react-native-lazy-index, something similar to below is generated:

const { AppRegistry } = require("react-native");

AppRegistry.registerComponent("SomeFeature", () => {
  // We'll import the module the first time "SomeFeature" is accessed.
  require("@awesome-app/some-feature");
  // "SomeFeature" is now overwritten and we can return the real component.
  // Subsequent calls to "SomeFeature" will no longer go through this wrapper.
  return AppRegistry.getRunnable("SomeFeature").componentProvider();
});

AppRegistry.registerComponent("AnotherFeature", () => {
  // We'll import the module the first time "AnotherFeature" is accessed.
  require("@awesome-app/another-feature");
  // "AnotherFeature" is now overwritten and we can return the real component.
  // Subsequent calls to "AnotherFeature" will no longer go through this
  // wrapper.
  return AppRegistry.getRunnable("AnotherFeature").componentProvider();
});

AppRegistry.registerComponent("YetAnotherFeature", () => {
  // We'll import the module the first time "YetAnotherFeature" is accessed.
  require("@awesome-app/yet-another-feature");
  // "YetAnotherFeature" is now overwritten and we can return the real
  // component. Subsequent calls to "YetAnotherFeature" will no longer go
  // through this wrapper.
  return AppRegistry.getRunnable("YetAnotherFeature").componentProvider();
});

AppRegistry.registerComponent("FinalFeature", () => {
  // We'll import the module the first time "FinalFeature" is accessed.
  require("@awesome-app/final-feature");
  // "FinalFeature" is now overwritten and we can return the real component.
  // Subsequent calls to "FinalFeature" will no longer go through this wrapper.
  return AppRegistry.getRunnable("FinalFeature").componentProvider();
});

Troubleshooting

If you're having trouble with undetected components, there are a couple of things you should look out for.

First parameter must be a string literal

react-native-lazy-index cannot evaluate the name passed to AppRegistry.registerComponent() or BatchedBridge.registerCallableModule() unless it is a string literal. For instance, if you have something like this in code:

const appName = "MyApp";

AppRegistry.registerComponent(appName, () => {
  ...
});

You'll need to inline the string:

AppRegistry.registerComponent("MyApp", () => {
  ...
});

react-native-lazy-index outputs warnings when it detects these instances.

My components are still not found

react-native-lazy-index avoids scanning dependencies too deeply to reduce its impact on the build time. If your registrations lie too deep within a dependency, it may have bailed out before reaching them. There are a couple of things you can do to help react-native-lazy-index find your components:

  1. If you have access to the source code, you can move your registrations further up, closer to the entry point of your dependency.
  2. You can increase the max depth by setting the environment variable RN_LAZY_INDEX_MAX_DEPTH. The default is currently set to 3. Note that changing this setting may significantly impact your build time.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].