Optimization: Re-use regexes in `getValueByKeys` because creating a new one every time is slow (#57)

0.2
Brian Chirls 2014-12-01 14:11:54 -05:00
rodzic 7a997a2f85
commit ae58cbc469
1 zmienionych plików z 9 dodań i 6 usunięć

Wyświetl plik

@ -5,7 +5,10 @@
/**
* Blueprint switchboard
* @author Robin Hawkes - vizicities.com
*/
*/
var arrayIndexRegEx = /\[(\d+)\]/,
arrayIndexRegExG = /\[(\d+)\]/g;
VIZI.BlueprintSwitchboard = function(config) {
var self = this;
@ -150,18 +153,18 @@
// Also retreives the value for "exampleObj.geometry[0]"
VIZI.BlueprintSwitchboard.prototype.getValueByKeys = function(object, keys) {
var output = object;
_.each(keys, function(key) {
if (!output) return null;
// Check for array reference in key
if (/\[(\d+)\]/.test(key)) {
if (arrayIndexRegEx.test(key)) {
var arrayKey = key.split("[")[0];
var arrayIndexRegEx = /\[(\d+)\]/g;
var arrayIndex;
while ((arrayIndex = arrayIndexRegEx.exec(key)) !== null) {
arrayIndexRegExG.lastIndex = 0;
while ((arrayIndex = arrayIndexRegExG.exec(key)) !== null) {
output = output[arrayKey][arrayIndex[1]];
}
// Else, assume key is not an array