Platform
Resources
Pricing
Sign in
Get started
Dylan Freedman
Senior software engineer at the Washington Post Creating Poly, a new programming language for the web (poly.dev)
Workspace
Fork
Published
By
Dylan Freedman
Edited
Mar 31, 2022
1 star
Insert cell
Insert cell
memoize
=
(
fn
)
=>
{
const
CACHE
=
{
}
;
return
(
...
args
)
=>
{
const
cacheKey
=
JSON
.
stringify
(
args
)
;
// Check if result is already cached
if
(
CACHE
[
cacheKey
]
!==
undefined
)
{
return
CACHE
[
cacheKey
]
;
}
// If not, compute the result
const
result
=
fn
(
...
args
)
;
// Cache the result and return
CACHE
[
cacheKey
]
=
result
;
return
result
;
}
;
}
Insert cell
Insert cell
function
sleep1Sec
(
returnValue
)
{
return
new
Promise
(
(
resolve
)
=>
{
setTimeout
(
(
)
=>
resolve
(
returnValue
)
,
1000
)
;
}
)
;
}
Insert cell
sleep1Sec
(
3
)
Insert cell
memoizeSleep1Sec
=
memoize
(
sleep1Sec
)
Insert cell
memoizeSleep1Sec
(
2
)
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
memoize
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
sleep1Sec
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
memoizeSleep1Sec
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML