Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0441e9d

Browse files
authored
Merge pull requestneetcode-gh#3735 from dmgodoy/feat/memoize-js
Create 2623-memoize.js
2 parentse71edf2 +0547031 commit0441e9d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎javascript/2623-memoize.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
*@param {Function} fn
3+
*@return {Function}
4+
*/
5+
functionmemoize(fn){
6+
letmemo={};
7+
returnfunction(...args){
8+
letargsjson=JSON.stringify(args);
9+
if(memo.hasOwnProperty(argsjson))
10+
returnmemo[argsjson];
11+
returnmemo[argsjson]=fn(...args);
12+
}
13+
}
14+
15+
/**
16+
* let callCount = 0;
17+
* const memoizedFn = memoize(function (a, b) {
18+
* callCount += 1;
19+
* return a + b;
20+
* })
21+
* memoizedFn(2, 3) // 5
22+
* memoizedFn(2, 3) // 5
23+
* console.log(callCount) // 1
24+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp