question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v3] Memoize most methods

See original GitHub issue

Feature 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:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mikehardycommented, Sep 8, 2019

And I learned something about async return values, default promise-boxing, interesting

1reaction
janicduplessiscommented, Sep 8, 2019

@mikehardy It is not needed for async functions, you can just return the value directly (Async functions implicitly wrap values with Promise.resolve).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found