Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Filter and filter by hand.
Browse files Browse the repository at this point in the history
  • Loading branch information
helio-frota committed Oct 4, 2016
1 parent 32cf07e commit c8d44e4
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ all: .
make -C class-prototype
make -C named_function-arrow_function
make -C hidden_class-no_extra_hidden_class
make -C foreach-for
make -C foreach-for
make -C filter-filter_by_hand
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [Named function and arrow function](named_function-arrow_function/#readme)
- [Hidden class and No extra hidden class](hidden_class-no_extra_hidden_class/#readme)
- [forEach and for](foreach-for/#readme)
- [filter and filter by hand](filter-filter_by_hand/#readme)

### How to push a test case

Expand Down
3 changes: 3 additions & 0 deletions filter-filter_by_hand/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "semistandard"
}
47 changes: 47 additions & 0 deletions filter-filter_by_hand/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
bench: lint
npm run benchmark > out.txt
npm run benchmark > out.txt
npm run benchmark > out.txt
tail -n 23 out.txt > bench.txt
rm out.txt
node --prof prof-filter.js > /dev/null
node --prof-process *.log > out.txt
grep -A 5 "Summary" -R out.txt > tick-filter.txt
rm *.log && rm out.txt
node --prof prof-filter_by_hand.js > /dev/null
node --prof-process *.log > out.txt
grep -A 5 "Summary" -R out.txt > tick-filter_by_hand.txt
rm *.log && rm out.txt && touch README.md && rm README.md
echo '## Benchmark' >> README.md
echo ' ' >> README.md
echo '```' >> README.md
cat bench.txt >> README.md
echo '```' >> README.md
echo ' ' >> README.md
echo '## Profile summary' >> README.md
echo ' ' >> README.md
echo 'Tick-filter' >> README.md
echo '```' >> README.md
cat tick-filter.txt >> README.md
echo '```' >> README.md
echo ' ' >> README.md
echo 'Tick-filter_by_hand' >> README.md
echo '```' >> README.md
cat tick-filter_by_hand.txt >> README.md
echo '```' >> README.md
echo ' ' >> README.md
rm *.txt
lint: node_modules
npm run lint

clean:
rm -rf node_modules

node_modules: init
npm install

init: .
cp ../package.json .
cp ../.eslintrc.json .

.PHONY: init
50 changes: 50 additions & 0 deletions filter-filter_by_hand/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## Benchmark

```
filterByHand
Raw:
> 18203.796203796202
> 18307.69230769231
> 18639.36063936064
> 18612.38761238761
Average (mean) 18440.80919080919
filterr
Raw:
> 2040.959040959041
> 2050.949050949051
> 2050.949050949051
> 2046.9530469530469
Average (mean) 2047.4525474525476
Winner: filterByHand
Compared with next highest (filterr), it's:
88.9% faster
9.01 times as fast
0.95 order(s) of magnitude faster
QUITE A BIT FASTER
```

## Profile summary

Tick-filter
```
[Summary]:
ticks total nonlib name
3123 32.9% 33.2% JavaScript
5638 59.4% 59.9% C++
330 3.5% 3.5% GC
85 0.9% Shared libraries
```

Tick-filter_by_hand
```
[Summary]:
ticks total nonlib name
7675 82.0% 87.3% JavaScript
516 5.5% 5.9% C++
232 2.5% 2.6% GC
564 6.0% Shared libraries
```

29 changes: 29 additions & 0 deletions filter-filter_by_hand/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const games = [
{ name: 'Doom', year: 1993 },
{ name: 'Golden Axe', year: 1989 },
{ name: 'Gauntlet', year: 1985 },
{ name: 'Frostbite', year: 1983 },
{ name: 'Tiger Heli', year: 1985 }
];

function filterByHand () {
let oldGames = [];
for (let i = 0; i < games.length; i++) {
if (games[i].year <= 1985) {
oldGames.push(games[i]);
}
}
}

exports.compare = {
'filterr': function () {
let weAreOld = games.filter(g => g.year <= 1985); // eslint-disable-line
},
'filterByHand': function () {
filterByHand();
}
};

require('bench').runMain();
19 changes: 19 additions & 0 deletions filter-filter_by_hand/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "perf-quest",
"version": "0.0.1",
"description": "Comparison between js features.",
"main": "index.js",
"scripts": {
"lint": "eslint .",
"benchmark": "node benchmark.js"
},
"devDependencies": {
"bench": "^0.3.6",
"eslint": "^3.3.1",
"eslint-config-semistandard": "^7.0.0-beta.0",
"eslint-config-standard": "^6.0.0",
"eslint-plugin-promise": "^2.0.1",
"eslint-plugin-react": "^6.1.2",
"eslint-plugin-standard": "^2.0.0"
}
}
20 changes: 20 additions & 0 deletions filter-filter_by_hand/prof-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const games = [
{ name: 'Doom', year: 1993 },
{ name: 'Golden Axe', year: 1989 },
{ name: 'Gauntlet', year: 1985 },
{ name: 'Frostbite', year: 1983 },
{ name: 'Tiger Heli', year: 1985 }
];

exports.compare = {
'filterr1': function () {
let weAreOld = games.filter(g => g.year <= 1985); // eslint-disable-line
},
'filterr2': function () {
let weAreOld = games.filter(g => g.year <= 1985); // eslint-disable-line
}
};

require('bench').runMain();
29 changes: 29 additions & 0 deletions filter-filter_by_hand/prof-filter_by_hand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const games = [
{ name: 'Doom', year: 1993 },
{ name: 'Golden Axe', year: 1989 },
{ name: 'Gauntlet', year: 1985 },
{ name: 'Frostbite', year: 1983 },
{ name: 'Tiger Heli', year: 1985 }
];

function filterByHand () {
let oldGames = [];
for (let i = 0; i < games.length; i++) {
if (games[i].year <= 1985) {
oldGames.push(games[i]);
}
}
}

exports.compare = {
'filterByHand1': function () {
filterByHand();
},
'filterByHand2': function () {
filterByHand();
}
};

require('bench').runMain();

0 comments on commit c8d44e4

Please sign in to comment.