From 1684ce2e5e623915ba2efd2d5528cc32506801ce Mon Sep 17 00:00:00 2001 From: PinkR1ver <3180102330@zju.edu.cn> Date: Sun, 31 Mar 2024 15:32:04 +0800 Subject: [PATCH] Add note --- .../coding_knowledge/js/memoization.md | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 content/computer_sci/coding_knowledge/js/memoization.md diff --git a/content/computer_sci/coding_knowledge/js/memoization.md b/content/computer_sci/coding_knowledge/js/memoization.md new file mode 100644 index 000000000..83bb48bb2 --- /dev/null +++ b/content/computer_sci/coding_knowledge/js/memoization.md @@ -0,0 +1,64 @@ +--- +title: Memoization +tags: + - javascript + - advanced + - coding-language +date: 2024-03-31 +--- +**记忆化**(Memoization)是一种优化技术,通过存储函数的计算结果,避免重复计算相同参数的函数值,从而提高代码的执行效率。 + + +> [!note] +> 值得注意的是,记忆化 仅对 纯函数 有效。纯函数的定义为:给定相同的输入,始终返回相同的输出,并且没有任何副作用的函数。 +> +> 例如,假设你尝试记忆化不纯的函数 Date.now,它返回自 Unix 时间戳以来的当前时间(以毫秒为单位)。 +> +> 第一次调用时会正确返回当前时间。但每次后续调用时,它都会错误地返回和第一次相同的值。 + +# Example + +记忆化有在React中运用 + +```js +const TitleComponent = (props) => { + return