Skip to content

Commit 9b9c386

Browse files
committed
Initial commit
0 parents  commit 9b9c386

8 files changed

+234
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
charset = utf-8
8+
indent_style = space
9+
indent_size = 2

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
node_modules
3+
out
4+
dist
5+
npm-debug.log
6+
deploy_key

CONTRIBUTING.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Contributing to this proposal
2+
First off, thank you for taking the time to contribute! 🎉
3+
4+
Here are some suggestions to contributing to this proposal.
5+
6+
1. The general [TC39 Process][], which summarizes
7+
how TC39’s “consensus” and “Stages” work.
8+
2. The guide on [contributing to TC39 proposals][contributing guide].
9+
3. The [TC39 Code of Conduct][CoC]:
10+
It has important information about how we’re all expected to act
11+
and what to do when we feel like someone’s conduct does not meet the Code.
12+
We all want to maintain a friendly, productive working environment!
13+
4. The [TC39 How to Give Feedback][feedback] article.
14+
5. The [proposal explainer][] to make sure that it is
15+
not already addressed there.
16+
6. The [TC39 Matrix guide][] (if you want to chat with TC39 members on Matrix,
17+
which is a real-time chat platform).
18+
7. If the explainer does not already explain your topic adequately,
19+
then please [search the GitHub repository’s issues][issues]
20+
to see if any issues match the topic you had in mind.
21+
This proposal is more than four years old,
22+
and it is likely that the topic has already been raised and thoroughly discussed.
23+
24+
You can leave a comment on an [existing GitHub issue][issues],
25+
create a new issue (but do try to [find an existing GitHub issue][issues] first),
26+
or [participate on Matrix][TC39 Matrix guide].
27+
28+
Please try to keep any existing GitHub issues on their original topic.
29+
30+
If you feel that someone’s conduct is not meeting the [TC39 Code of Conduct][CoC],
31+
whether in this GitHub repository or in a [TC39 Matrix room][TC39 Matrix guide],
32+
then please follow the [Code of Conduct][CoC]’s directions for reporting the violation,
33+
including emailing [[email protected]][].
34+
35+
Thank you again for taking the time to contribute!
36+
37+
[CoC]: https://tc39.es/code-of-conduct/
38+
[TC39 process]: https://tc39.es/process-document/
39+
[contributing guide]: https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#new-feature-proposals
40+
[feedback]: https://github.com/tc39/how-we-work/blob/master/feedback.md
41+
[proposal explainer]: https://github.com/tc39/proposal-array-from-async/blob/main/README.md
42+
[TC39 Matrix guide]: https://github.com/tc39/how-we-work/blob/master/matrix-guide.md
43+
[issues]: https://github.com/tc39/proposal-array-from-async/issues?q=is%3Aissue+
44+

HISTORY.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Brief history of ES Function.prototype.memo
2+
For information on what “Stage 1” and “Stage 2” mean,
3+
read about the [TC39 Process][].
4+
5+
More information about contributing is also available in [CONTRIBUTING.md][].
6+
7+
## 2015–2021
8+
The pipe champion group presents F# pipes (a tacit-unary-function-application
9+
operator) for Stage 2 twice to TC39, being unsuccessful both times due to
10+
pushback from multiple other TC39 representatives’ memory performance concerns,
11+
syntax concerns about await, and concerns about encouraging ecosystem
12+
bifurcation/forking.
13+
14+
For more information, see the [pipe proposal’s HISTORY.md][pipe history].
15+
16+
## 2021-09
17+
Inspired by [pipe issue #233][], [@js-choi][] creates a new proposal
18+
that would add several Function helper methods.
19+
20+
## 2021-10
21+
[On 2021-10, proposal-function-helpers is presented to the Committee
22+
plenary][2021-10] for Stage 1. The Committee rejects the proposal due to its
23+
being overly broad and requests that it be split up into multiple proposals.
24+
25+
## 2022-03
26+
[On 2022-03, a TC39 incubator meeting is held][2022-03 incubator] to discuss
27+
Function helpers in general. Several representatives express support for
28+
Function.prototype.once, and it is decided to pursue Function.prototype.once
29+
for Stage 1.
30+
31+
[TC39 process]: https://tc39.es/process-document/
32+
[CONTRIBUTING.md]: https://github.com/tc39/proposal-pipeline-operator/blob/main/CONTRIBUTING.md
33+
34+
[pipe history]: https://github.com/tc39/proposal-pipeline-operator/blob/main/HISTORY.md
35+
[pipe issue #233]: https://github.com/tc39/proposal-pipeline-operator/issues/233
36+
37+
[@js-choi]: https://github.com/js-choi
38+
39+
[2021-10]: https://github.com/tc39-transfer/proposal-function-helpers/issues/17#issuecomment-953814353
40+
41+
[2022-03 incubator]: https://github.com/tc39/incubator-agendas/blob/main/notes/2022/03-08.md

LICENSE.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Copyright 2022 J. S. Choi
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions
5+
are met:
6+
7+
1. Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
14+
3. Neither the name of the copyright holder nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
**This software is provided by the copyright holders and contributors
19+
“as is” and any express or implied warranties, including, but not
20+
limited to, the implied warranties of merchantability and fitness for a
21+
particular purpose are disclaimed. In no event shall the copyright
22+
holder or contributors be liable for any direct, indirect, incidental,
23+
special, exemplary, or consequential damages (including, but not limited
24+
to, procurement of substitute goods or services; loss of use, data, or
25+
profits; or business interruption) however caused and on any theory of
26+
liability, whether in contract, strict liability, or tort (including
27+
negligence or otherwise) arising in any way out of the use of this
28+
software, even if advised of the possibility of such damage.**

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Function.prototype.memo for JavaScript
2+
ECMAScript Stage-0 Proposal. J. S. Choi, 2022.
3+
4+
## Rationale
5+
[Function memoization][] is a common technique that caches the results of
6+
function calls and returns the cached results when the same inputs occur again.
7+
These are useful for:
8+
9+
* Optimizing expensive function calls.
10+
* Ensuring that callbacks always return the same [singleton object][].
11+
* [Mutually recursive][] [recursive-descent parsing][].
12+
13+
[function memoization]: https://en.wikipedia.org/wiki/Memoization
14+
[singleton object]: https://en.wikipedia.org/wiki/Singleton_pattern
15+
[mutually recursive]: https://en.wikipedia.org/wiki/Mutual_recursion
16+
[recursive-descente parsing]: https://en.wikipedia.org/wiki/Recursive_descent_parser
17+
18+
Memoization is common but annoying to write. This proposal would add a standard
19+
memoization to the core JavaScript language. Its caching system would be based on WeakMaps and tuples.
20+
21+
## Description
22+
The Function.prototype.memo method would create a new function that calls the
23+
original function at most once for each tuple of given arguments. Any
24+
subsequent calls to the new function with identical arguments would return the
25+
result of the first call with those arguments.
26+
27+
```js
28+
function f (x) { console.log(x); return x * 2; }
29+
30+
const fMemo = f.memo();
31+
fMemo(3); // Prints 3 and returns 6.
32+
fMemo(3); // Does not print anything. Returns 6.
33+
fMemo(2); // Prints 2 and returns 4.
34+
fMemo(2); // Does not print anything. Returns 4.
35+
fMemo(3); // Does not print anything. Returns 6.
36+
```
37+
38+
## Unresolved questions
39+
How should cache garbage collection work? (Using WeakMaps for the caches would
40+
be ideal…except that WeakMaps do not support primitives as keys.) Should we
41+
just use Maps and make the developer manage the cache memory themselves? This
42+
problem is unresolved.
43+
44+
If we go with a Map cache, how should we structure the cache? For example, we
45+
could use a tree of Maps, or we could use argument-[tuples][] as keys in one
46+
Map.
47+
48+
[tuples]: https://github.com/tc39/proposal-record-tuple
49+
50+
Should we add an LRUMap ([least recently used][]) to the language, like
51+
[Python’s lru_cache][Python functools.lru_cache]? If so, should it be added
52+
with this proposal?
53+
54+
[least recently used]: https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)
55+
56+
Should `memo` be a prototype method, a static function, a function decorator,
57+
or multiple things?
58+
59+
## Precedents
60+
61+
* [lodash.memoize](https://lodash.com/docs/4.17.15#memoize)
62+
* [Undescore.js memoize](https://underscorejs.org/#memoize)
63+
* [Python functools.lru_cache][]
64+
* [Wikipedia “function memoization” article][function memoization]
65+
66+
[Python functools.lru_cache]: https://docs.python.org/3/library/functools.html#functools.lru_cache

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "proposal-function-memo",
3+
"private": true,
4+
"description": "A TC39 proposal for an Function.prototype.memo method in the JavaScript language.",
5+
"author": "J. S. Choi <[email protected]> (https://jschoi.org/)",
6+
"license": "BSD-3-Clause",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/tc39/proposal-array-async-from.git"
10+
},
11+
"keywords": [
12+
"proposal",
13+
"tc39",
14+
"function",
15+
"callback"
16+
],
17+
"scripts": {
18+
"prebuild": "mkdir -p dist",
19+
"build": "ecmarkup --verbose spec.html dist/index.html --css dist/ecmarkup.css --js dist/ecmarkup.js",
20+
"watch": "npm run build -- --watch"
21+
},
22+
"devDependencies": {
23+
"ecmarkup": "^10.0.2"
24+
},
25+
"homepage": "https://github.com/js-choi/proposal-function-memo"
26+
}

spec.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<pre class=metadata>
2+
title: ES Function.prototype.memo (2021)
3+
status: proposal
4+
stage: 0
5+
location: https://github.com/js-choi/proposal-function-memo
6+
copyright: false
7+
contributors: J. S. Choi
8+
</pre>
9+
<script src=ecmarkup.js defer></script>
10+
<link rel=stylesheet href=ecmarkup.css>
11+
12+
<emu-intro id=introduction>
13+
<h1>Introduction</h1>
14+
</emu-intro>

0 commit comments

Comments
 (0)