Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmark test suite #104

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bench.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>react-redux benchmark</title>
</head>
<body>
<pre id="res">Loading js...</pre>
<div id="app"></div>
<script src="node_modules/benchmark/benchmark.js" charset="utf-8"></script>
<script src="node_modules/react/dist/react.min.js" charset="utf-8"></script>
<script src="node_modules/react/dist/react-dom.min.js" charset="utf-8"></script>
<script src="node_modules/redux/dist/redux.min.js" charset="utf-8"></script>
<script src="dist/react-redux.min.js" charset="utf-8"></script>
<script src="bench.js" charset="utf-8"></script>
</body>

</html>
69 changes: 69 additions & 0 deletions bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
var suite = new Benchmark.Suite;

var benchRes = document.getElementById('res');

function reducer(state, action) {
if (!state) state = {toggle: false};
switch (action.type) {
case 'TOGGLE':
return {toggle: !state.toggle};
case 'RESET':
return {toggle: false};
default:
return state;
}
}

var store = Redux.createStore(reducer);

var Toggle = React.createClass({

render: function() {
return React.createElement('div', null, [this.props.yes ? 'yes' : 'no']);
},

});

Toggle = ReactRedux.connect(function(state) {
return {yes: state.toggle};
})(Toggle);


ReactDOM.render(
React.createElement(
ReactRedux.Provider,
{store: store},
React.createElement(
"div",
null,
Array.apply(null, {length: 333}).map(function(_, i) {
return React.createElement(Toggle, {key: i})
})
)
), document.getElementById('app')
);


suite
.add('dispatch', function() {
store.dispatch({type: 'TOGGLE'});
})
.add('dispatch batched', function() {
ReactDOM.unstable_batchedUpdates(function() {
store.dispatch({type: 'TOGGLE'});
});
})
.on('cycle', function(event) {
benchRes.innerHTML += String(event.target) + "\n";
})
.on('complete', function(e) {
benchRes.innerHTML += '\n';
benchRes.innerHTML += 'All done!\n\nFastest was: ' + this.filter('fastest').pluck('name');
benchRes.innerHTML += '\n';
})
.on('start', function() {
benchRes.innerHTML = 'Executing the benchmark...\n\n';
})
// run async
.run({ 'async': true });

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"clean": "rimraf lib dist coverage",
"lint": "eslint src test",
"prepublish": "npm run clean && npm run build",
"bench": "npm run build && python -m SimpleHTTPServer",
"test": "mocha --compilers js:babel/register --recursive",
"test:watch": "npm test -- --watch",
"test:cov": "babel-node ./node_modules/isparta/bin/isparta cover ./node_modules/mocha/bin/_mocha -- --recursive"
Expand Down Expand Up @@ -41,6 +42,7 @@
"babel-core": "^5.8.22",
"babel-eslint": "^3.1.15",
"babel-loader": "^5.3.2",
"benchmark": "^1.0.0",
"eslint": "^0.23",
"eslint-config-airbnb": "0.0.6",
"eslint-plugin-react": "^2.3.0",
Expand Down