Skip to content

Commit 4da9b8e

Browse files
committed
Merge branch 'adaptative-memoize'
* adaptative-memoize: (45 commits) Moar fixes Remove mspack-lite from serializer benchmark Removes mocha package E_TOO_MUCH_CODE Rename table header on `benchmark/increasing-n.js` Enable coverage on jest `...args` is faster than `arguments` Better benchmark output Refactor index to accordingly to the stepdown rule Removes underscore dangle Rename `_name` to `label` Add benchmar for strategies Add benchmark for cache Add partial application strategy New icon Change linter to ESLint Remove webpack and karma reminiscent files Fix license Add files field to package.json Migrate test runner to jest ...
2 parents 4153279 + 30b5386 commit 4da9b8e

38 files changed

+1185
-756
lines changed

README.md

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,34 +104,14 @@ npm run benchmark
104104

105105
<img src="http://rawgit.com/caiogondim/fast-memoize/master/img/benchmark-cli.png" width="100%" />
106106

107-
## Support
108-
109-
### Desktop browsers
110-
111-
| ![Chrome](https://raw.github.com/alrra/browser-logos/master/chrome/chrome_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/internet-explorer/internet-explorer_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/firefox/firefox_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/safari/safari_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/opera/opera_48x48.png) | ![Edge](https://raw.github.com/alrra/browser-logos/master/edge/edge_48x48.png) | ![Brave](https://raw.github.com/alrra/browser-logos/master/brave/brave_48x48.png) |
112-
| --- | --- | --- | --- | --- | --- | --- |
113-
| Latest | 8+ | Latest | Latest | Latest | Latest | Latest |
114-
115-
### Mobile browsers
116-
117-
| ![Chrome](https://raw.github.com/alrra/browser-logos/master/chrome/chrome_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/safari-ios/safari-ios_48x48.png) | ![Android Browser](https://raw.github.com/alrra/browser-logos/master/android/android_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/internet-explorer/internet-explorer_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/firefox/firefox_48x48.png) | ![Opera](https://raw.github.com/alrra/browser-logos/master/opera/opera_48x48.png) | ![UC](https://raw.github.com/alrra/browser-logos/master/uc/uc_48x48.png) |
118-
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
119-
| Latest | 6+ | 4.0+ | 8+ | Latest | Latest | Latest |
120-
121-
### Server
122-
123-
| <a href="https://nodejs.org"><img height=48 src="https://raw.githubusercontent.com/caiogondim/javascript-server-side-logos/master/node.js/standard/454x128.png"></a> |
124-
| --- |
125-
| 0.10+ ✔ |
126-
127107
## Reference
128108
- https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments
129109

130110
## Credits
131111
- Icon by Mary Rankin from the Noun Project
132112
- [Bullet train ZSH theme](https://github.com/caiogondim/bullet-train-oh-my-zsh-theme)
133113

134-
---
114+
---
135115

136116
[caiogondim.com](https://caiogondim.com) &nbsp;&middot;&nbsp;
137117
GitHub [@caiogondim](https://github.com/caiogondim) &nbsp;&middot;&nbsp;

TODO.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
1-
- [ ] Fix single argument case for non primitive argument
2-
- [ ] Fix lint task
1+
- [ ] 100% coverage
2+
- [ ] Implement "explicit" strategy
3+
- named params
4+
- npm scripts
5+
6+
## Steps
7+
- Inline
8+
- Explicit strategy
9+
- NodeJS optimizations
10+
- instrospection
11+
- Enable turbo
12+
- web tracing framework
13+
14+
---
15+
16+
- bind with args
17+
- has prop
18+
- reflect api
19+
- in
20+
- hasOwnProperty
21+
- !== undefined
22+
23+
---
24+
25+
has !== undefined is super slow
26+
27+
analise do algoritmo fibonacci
28+
29+
https://blog.ghaiklor.com/tracing-de-optimizations-in-nodejs-2ba16900fc6f#.r6hi36wku
30+
https://www.npmjs.com/package/v8-natives
31+
http://blog.trevnorris.com/2013/07/measuring-node-performance-part-1.html
32+
http://mrale.ph/s3/nodecamp.eu/#1
33+
https://floitsch.blogspot.com.br/2012/03/optimizing-for-v8-introduction.html
34+
35+
36+
https://github.com/petkaantonov/bluebird/wiki/Optimization-killers
37+
https://www.npmjs.com/package/v8-natives
38+
--turbo
39+
https://gist.github.com/kevincennis/0cd2138c78a07412ef21
40+
41+
https://docs.google.com/presentation/d/1Lq2DD28CGa7bxawVH_2OcmyiTiBn74dvC6vn2essroY/edit#slide=id.g1a504e63c9_2_84

benchmark/addy-osmani.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

benchmark/cache/index.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const ora = require('ora')
2+
const Table = require('cli-table2')
3+
const debug = require('logdown')()
4+
const Benchmark = require('benchmark')
5+
6+
const results = []
7+
const spinner = ora('Running benchmark')
8+
9+
//
10+
// View
11+
//
12+
13+
function showResults (benchmarkResults) {
14+
const table = new Table({head: ['NAME', 'OPS/SEC', 'RELATIVE MARGIN OF ERROR', 'SAMPLE SIZE']})
15+
benchmarkResults.forEach((result) => {
16+
table.push([
17+
result.target.name,
18+
result.target.hz.toLocaleString('en-US', {maximumFractionDigits: 0}),
19+
${result.target.stats.rme.toFixed(2)}%`,
20+
result.target.stats.sample.length
21+
])
22+
})
23+
24+
debug.log(table.toString())
25+
}
26+
27+
function sortDescResults (benchmarkResults) {
28+
return benchmarkResults.sort((a, b) => a.target.hz < b.target.hz ? 1 : -1)
29+
}
30+
31+
function onCycle (event) {
32+
results.push(event)
33+
ora(event.target.name).succeed()
34+
}
35+
36+
function onComplete () {
37+
spinner.stop()
38+
debug.log()
39+
40+
const orderedBenchmarkResults = sortDescResults(results)
41+
showResults(orderedBenchmarkResults)
42+
}
43+
44+
spinner.start()
45+
46+
//
47+
// Benchmark
48+
//
49+
50+
let fibonacci = (n) => {
51+
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2)
52+
}
53+
54+
let caches = []
55+
caches.push(require('./map'))
56+
caches.push(require('./object'))
57+
caches.push(require('./object-without-prototype'))
58+
caches.push(require('./lru-cache'))
59+
60+
let serializers = []
61+
serializers.push(require('../serializer/json-stringify'))
62+
63+
let strategies = []
64+
strategies.push(require('../strategy/partial-application'))
65+
66+
let memoizedFunctions = []
67+
strategies.forEach(function (strategy) {
68+
serializers.forEach(function (serializer) {
69+
caches.forEach(function (cache) {
70+
let memoizedFibonacci = strategy(fibonacci, {cache, serializer})
71+
memoizedFibonacci.label = cache.label
72+
memoizedFunctions.push(memoizedFibonacci)
73+
})
74+
})
75+
})
76+
77+
let suiteFibonnaci = new Benchmark.Suite()
78+
let fibNumber = 15
79+
80+
memoizedFunctions.forEach(function (memoizedFunction) {
81+
suiteFibonnaci.add(memoizedFunction.label, () => memoizedFunction(fibNumber))
82+
})
83+
84+
suiteFibonnaci
85+
.on('cycle', onCycle)
86+
.on('complete', onComplete)
87+
.run({'async': true})

benchmark/cache/isaacs-lru-cache.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

benchmark/cache/lru-cache.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var LruCache = require('lru-cache')
2+
3+
module.exports = {
4+
create: () => new LruCache(),
5+
label: 'lru-cache'
6+
}

benchmark/cache/map.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
'use strict'
1+
function hasSupport () {
2+
var hasSupport = true
23

3-
class MapCache extends Map {
4-
constructor () {
5-
super()
6-
this._name = 'Map'
4+
try {
5+
var map = new Map()
6+
map.set(null)
7+
} catch (error) {
8+
hasSupport = false
79
}
10+
11+
return hasSupport
12+
}
13+
14+
function create () {
15+
var cache = new Map()
16+
return cache
817
}
918

10-
module.exports = MapCache
19+
module.exports = {
20+
create: create,
21+
hasSupport: hasSupport,
22+
label: 'Map'
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class ObjectWithoutPrototypeCache {
2+
constructor () {
3+
this.cache = Object.create(null)
4+
}
5+
6+
has (key) {
7+
return (key in this.cache)
8+
}
9+
10+
get (key) {
11+
return this.cache[key]
12+
}
13+
14+
set (key, value) {
15+
this.cache[key] = value
16+
}
17+
18+
delete (key) {
19+
delete this.cache[key]
20+
}
21+
}
22+
23+
module.exports = {
24+
create: () => new ObjectWithoutPrototypeCache(),
25+
label: 'Object without prototype'
26+
}

benchmark/cache/object.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
'use strict'
2-
31
class ObjectCache {
42
constructor () {
5-
this._cache = Object.create(null)
6-
this._name = 'Object'
3+
this.cache = {}
74
}
85

96
has (key) {
10-
return (key in this._cache)
7+
return (key in this.cache)
118
}
129

1310
get (key) {
14-
return this._cache[key]
11+
return this.cache[key]
1512
}
1613

1714
set (key, value) {
18-
this._cache[key] = value
15+
this.cache[key] = value
1916
}
2017

2118
delete (key) {
22-
delete this._cache[key]
19+
delete this.cache[key]
2320
}
2421
}
2522

26-
module.exports = ObjectCache
23+
module.exports = {
24+
create: () => new ObjectCache(),
25+
label: 'Object'
26+
}

0 commit comments

Comments
 (0)