Permalink
Browse files

[minor] Adding the fastest way of adding and retrieving data from an …

…array
  • Loading branch information...
1 parent ca4bb24 commit 138f34a68be820cbf03d2f62ea60246328daa4d5 @3rd-Eden committed Jan 27, 2013
Showing with 51 additions and 0 deletions.
  1. +51 −0 benchmark/micro/shiftunshiftpushpop.bench.js
@@ -0,0 +1,51 @@
+'use strict';
+
+/**
+ * Benchmark related modules.
+ */
+var benchmark = require('benchmark')
+ , microtime = require('microtime');
+
+/**
+ * Logger
+ */
+var logger = new(require('devnull'))({ timestamp: false, namespacing: 0 });
+
+/**
+ * Preparation code.
+ */
+var arr = []
+ , cursor = 0;
+
+(
+ new benchmark.Suite()
+).add('push & shift', function test1() {
+ arr.push(1);
+ var x = arr.shift();
+}).add('.length & shift', function test2() {
+ arr[arr.length] = 1;
+ var x = arr.shift();
+}).add('unshift & pop', function test3() {
+ arr.unshift(1);
+ var x = arr.pop();
+}).add('cursor, delete and push', function test4() {
+ arr.push(1);
+
+ var x = arr[cursor];
+ delete arr[cursor];
+ cursor++;
+}).on('cycle', function cycle(e) {
+ var details = e.target;
+
+ logger.log('Finished benchmarking: "%s"', details.name);
+ logger.metric('Count (%d), Cycles (%d), Elapsed (%d), Hz (%d)'
+ , details.count
+ , details.cycles
+ , details.times.elapsed
+ , details.hz
+ );
+}).on('complete', function completed() {
+ logger.info('Benchmark: "%s" is was the fastest.'
+ , this.filter('fastest').pluck('name')
+ );
+}).run();

0 comments on commit 138f34a

Please sign in to comment.