Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use memo to improve Javascript function

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use memos to improve Javascript functions", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use memos to improve Javascript functions.

What is a memo?

Memo is a form of caching and is a value storage method that is easy to enter and use later. The memo is to record the results of the solved problem so that you don't have to recalculate the next time you need to do the same thing again. However, for a function to use a memo, it must be referenced transparently, that is, it can only be used if the effect of calling the function is exactly the same as replacing the function call with the return value of the function.

There are memos in most programming languages such as Ruby, C++, Python, and so on, and there are even many libraries that can be simplified. In this article, we will focus on Javascript. Programming languages may be different, but the concepts and ideas are the same.

The concept of memo

The memo needs to understand the following two concepts:

1. Reference transparency

If an expression can be replaced by its corresponding value without changing the behavior of the program (and vice versa), it is called reference transparency. This requires that the expression must be pure, that is, for the same input, the value of the expression must be the same, and its evaluation must have no side effects. Expressions that are not reference transparent are called reference opacity.

With the above explanation, we can quickly associate it with the behavior of the memo, a concept that allows us to write a function with a memo.

two。 Look-up table

The lookup table (LUT) is an array that replaces run-time calculations with simpler array index operations. This process is called "direct addressing," and LUT differs from a hash table in that it retrieves a value.

Compare functions using memos and not using memos

Take the classic Fibonacci series definition as an example:

Function Fibo (n) {if (n)

< 2) { return n; } return Fibo(n - 1) + Fibo(n - 2); } 你可能预料到了,一旦开始使用大于20的数字,这个过程就会变得非常缓慢。而当你处理35左右的数字时,计算机估计也撑不住了。 解决方法是记录调用函数的返回结果var IterMemoFib = function() { var cache = [1, 1]; var fib = function(n) { if (n >

= cache.length) {for (var I = cache.length; I 0? Arguments [I]: null; var memoizedFn = function () {/ / Copy the arguments object into an array: allows it to be used as / / a cache key. Var args = []; for (var I = 0; I < arguments.length; iTunes +) {args [I] = arguments [I];} / / Evaluate the memoized function if it hasn't been evaluated with / / these arguments before. If (! (args in pad)) {pad [args] = self.apply (obj, arguments);} return pad [args];}; memoizedFn.unmemoize = function () {return self;}; return memoizedFn;}; Function.prototype.unmemoize = function () {alert ("Attempt to unmemoize an unmemoized function."); return null;}; meaning of the memorandum

Remember the result that corresponds to a specific set of inputs

The memo reduces the time cost of the function in exchange for space cost.

Memos don't rely on machines.

At this point, I believe you have a deeper understanding of "how to use memos to improve Javascript functions". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report