Skip to content

Commit

Permalink
Merge pull request #4 from truck-js/docs/benchmark
Browse files Browse the repository at this point in the history
docs: add benchmark test
  • Loading branch information
hvolschenk committed Dec 20, 2018
2 parents 7f312e2 + c3fb161 commit fee6d08
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ Unit testing can be run in isolation through the command:
$ docker-compose run --rm app npm run test:scripts
```

### Benchmarking

Although not part of the CI test suite, a _Benchmark_ test is available to see how _Queue_ performs
against a standard array with 1,000,000 items.

The _Benchmark_ test can be run with the following command:

```sh
$ docker-compose run --rm app npm run test:benchmark
```

Optionally, the amount of items can be set by passing in the amount to the script:

```sh
$ docker-compose run --rm app npm run test:benchmark 65535
```

## Contributing

Contributions are always welcome, just submit a PR to get the conversation going. Please make sure
Expand Down
43 changes: 43 additions & 0 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const Benchmark = require('benchmark');

// eslint-disable-next-line import/no-unresolved
const Stack = require('../lib/index').default;

const suite = new Benchmark.Suite();

const testLength = process.argv[2] || 1000000;
const stack = new Stack(testLength);
const array = new Array(testLength);

for (let length = 0; length < testLength; length += 1) {
stack.push(length);
array.push(length);
}

process.stdout.write(`\nRunning benchmark with ${testLength} items:\n\n`);

suite
.add('Array#pop() ', () => {
const a = array.pop();
const b = array.pop();
const c = array.pop();
array.push(a);
array.push(b);
array.push(c);
})
.add('Stack#pop() ', () => {
const a = stack.pop();
const b = stack.pop();
const c = stack.pop();
stack.push(a);
stack.push(b);
stack.push(c);
})
.on('cycle', (event) => {
process.stdout.write(`${String(event.target)}\n`);
})
.on('complete', function complete() {
process.stdout.write(`Fastest is ${this.filter('fastest').map('name')}\n`);
})
.run({ async: true });
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
"version": "0.0.0-development",
"description": "A JavaScript implementation of the Stack data structure",
"main": "lib/index.js",
"files": ["/lib"],
"files": [
"/lib"
],
"scripts": {
"build": "babel src --out-dir lib --ignore src/**/*.test.js",
"coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls",
"semantic-release": "semantic-release",
"test": "npm run test:lint && npm run test:vulnerabilities && npm run test:scripts",
"test:benchmark": "npm run build && node benchmark/index.js",
"test:lint": "eslint --ext js .",
"test:scripts": "jest --config ./jest.config.json --coverage",
"test:vulnerabilities": "npm audit",
Expand All @@ -28,6 +31,7 @@
"@commitlint/config-conventional": "^7.1.2",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"benchmark": "^2.1.4",
"coveralls": "^3.0.2",
"eslint": "^5.9.0",
"eslint-config-airbnb-base": "^13.1.0",
Expand Down

0 comments on commit fee6d08

Please sign in to comment.