hapijs/hoek — pull request #115 by danielb2 (2014-12-05 20:05:50+00:00)
Title: Adds ability to do Hoek.reach with negative index

Fixes #114

So I did benchmark this with three different versions.

Version 1 was plain without negative index.
Version 2 used an initial check to see if there's any negative in the string. This would only be expected to make a difference in a very large reach anyway.
Version 3 just does a check for a negative for each part of the path (v2 in test)

The result was this:

```
[~/tmp]$ time node negative_index.js
get with index x 3,288,783 ops/sec ±0.24% (1092 runs sampled)
get with negative index x 3,297,145 ops/sec ±0.23% (1094 runs sampled)
get with negative index v2 x 3,314,562 ops/sec ±0.20% (1095 runs sampled)
Fastest is get with negative index v2
383.03 real 178.94 user 1.30 sys
```

See below for the code for the test. let me know if you'd like me to test it some other way, but I thought with 1000 samples something would have come up if it was significantly slower.

``` javascript

var Hoek = require('hoek');
var HoekNeg = require('hoek-neg');
var HoekNeg2 = require('hoek-neg2');
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;

var a = [1, 2, 3, 4, 5, 6, 7, 8];
suite.add('get with index', function () {

var b = Hoek.reach(a, '0');
})

suite.add('get with negative index', function () {

var b = Hoek.reach(a, '0');
})

suite.add('get with negative index v2', function () {

var b = Hoek.reach(a, '0');
})

suite.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
});

suite.on('cycle', function (event) {
console.log(String(event.target));
})

suite.run({ async: true, minSamples: 1000 });
```