[v3] Memoize most methods
See original GitHub issueFeature Request
Now that v3 doesn’t use constants anymore I think it would be a good idea to memoize values in JS to avoid having to go through the bridge for each repeated call. Most values returned by this library never change so it would work well.
Why it is needed
This would lead to better performance for repeated calls to the same method. The performance characteristics would be closer to v2 since it used to be only a js property access and not a bridge call.
Possible implementation
-
Add a field per method to store the value returned, then use that value for subsequent calls instead of going through the bridge. Main benefit of this is we can use the same cached value for the sync and async method.
-
Use some memoization library or create our own higher order function.
Code sample
let _deviceId;
export async function getDeviceId() {
if (_deviceId === undefined) {
if (OS === 'android' || OS === 'ios' || OS === 'windows') {
_deviceId = await RNDeviceInfo.getDeviceId();
} else {
_deviceId = 'unknown';
}
}
return _deviceId;
}
export function getDeviceIdSync() {
if (_deviceId === undefined) {
if (OS === 'android' || OS === 'ios' || OS === 'windows') {
_deviceId = RNDeviceInfo.getDeviceIdSync();
} else {
_deviceId = 'unknown';
}
}
return _deviceId;
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Understanding Memoization.. One of the most ... - Medium
Today, we will try to understand the concept and why it is highly rated as one of the most optimized ways of performance...
Read more >What is Memoization? How and When to ... - freeCodeCamp
In this article we will talk about memoization, an optimization technique that can help make heavy computation processes more efficient.
Read more >Forever Functional: Memoizing Functions for Performance
In agile methodologies, the Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, ... A cache map is defined at (1) -and using...
Read more >Memoization: What, Why, and How | Kyle Shevlin
Memoization is a technique that enhances a function by creating and using a cache to store and retrieve results of that function. Memoization...
Read more >Understanding JavaScript Memoization In 3 Minutes - codeburst
... that talk about the optimization technique called memoization. ... what memoization is doing by looking at a very simple and impractical ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
And I learned something about async return values, default promise-boxing, interesting
@mikehardy It is not needed for async functions, you can just return the value directly (Async functions implicitly wrap values with Promise.resolve).