Skip to content

Commit

Permalink
memoize split strings to reduce garbage; fix matching bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
machenmusik committed Mar 23, 2018
1 parent a1c704a commit 5998faf
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/utils/tracked-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ var DEFAULT_HANDEDNESS = require('../constants').DEFAULT_HANDEDNESS;
var AXIS_LABELS = ['x', 'y', 'z', 'w'];
var NUM_HANDS = 2; // Number of hands in a pair. Should always be 2.

var splitOnPipeMap = {};

function splitOnPipeOnce (stringToSplit) {
var rtn = splitOnPipeMap[stringToSplit];
if (!rtn) {
rtn = stringToSplit.split('|');
splitOnPipeMap[stringToSplit] = rtn;
}
return rtn;
}

/**
* Called on controller component `.play` handlers.
* Check if controller matches parameters and inject tracked-controls component.
Expand Down Expand Up @@ -90,19 +101,23 @@ function findMatchingController (controllers, filterIdExact, filterIdPrefix, fil
var targetControllerMatch = filterControllerIndex || 0;
var filterIdPrefixes;
if (filterIdPrefix && filterIdPrefix.indexOf('|') >= 0) {
filterIdPrefixes = filterIdPrefix.split('|');
filterIdPrefixes = splitOnPipeOnce(filterIdPrefix);
}
for (i = 0; i < controllers.length; i++) {
controller = controllers[i];
// Determine if the controller ID matches our criteria
if (filterIdPrefixes) {
var matches = false;
for (var prefix in filterIdPrefixes) {
if (prefix && controller.id.indexOf(prefix) === -1) { matches = true; }
for (var j = 0; j < filterIdPrefixes.length; j++) {
var prefix = filterIdPrefixes[j];
if (prefix && controller.id.indexOf(prefix) === 0) {
matches = true;
break;
}
}
if (!matches) { continue; }
} else
if (filterIdPrefix && controller.id.indexOf(filterIdPrefix) === -1) { continue; }
if (filterIdPrefix && controller.id.indexOf(filterIdPrefix) !== 0) { continue; }
if (!filterIdPrefix && controller.id !== filterIdExact) { continue; }

// If the hand filter and controller handedness are defined we compare them.
Expand Down

0 comments on commit 5998faf

Please sign in to comment.