Optimisation - Remove redunant lookups

It's surprising how much time it takes to do an object member lookup, so
reducing these is a great idea.
This commit is contained in:
griffpatch 2017-01-28 16:33:20 +00:00
parent 824628220c
commit 76c9c993f2

View file

@ -54,8 +54,8 @@ Blocks.prototype.getScripts = function () {
* @return {?string} ID of next block in the sequence * @return {?string} ID of next block in the sequence
*/ */
Blocks.prototype.getNextBlock = function (id) { Blocks.prototype.getNextBlock = function (id) {
if (typeof this._blocks[id] === 'undefined') return null; var block = this._blocks[id];
return this._blocks[id].next; return (typeof block === 'undefined') ? null : block.next;
}; };
/** /**
@ -85,8 +85,8 @@ Blocks.prototype.getBranch = function (id, branchNum) {
* @return {?string} the opcode corresponding to that block * @return {?string} the opcode corresponding to that block
*/ */
Blocks.prototype.getOpcode = function (id) { Blocks.prototype.getOpcode = function (id) {
if (typeof this._blocks[id] === 'undefined') return null; var block = this._blocks[id];
return this._blocks[id].opcode; return (typeof block === 'undefined') ? null : block.opcode;
}; };
/** /**
@ -95,8 +95,8 @@ Blocks.prototype.getOpcode = function (id) {
* @return {!Object} All fields and their values. * @return {!Object} All fields and their values.
*/ */
Blocks.prototype.getFields = function (id) { Blocks.prototype.getFields = function (id) {
if (typeof this._blocks[id] === 'undefined') return null; var block = this._blocks[id];
return this._blocks[id].fields; return (typeof block === 'undefined') ? null : block.fields;
}; };
/** /**
@ -105,13 +105,14 @@ Blocks.prototype.getFields = function (id) {
* @return {!Object} All non-branch inputs and their associated blocks. * @return {!Object} All non-branch inputs and their associated blocks.
*/ */
Blocks.prototype.getInputs = function (id) { Blocks.prototype.getInputs = function (id) {
if (typeof this._blocks[id] === 'undefined') return null; var block = this._blocks[id];
if (typeof block === 'undefined') return null;
var inputs = {}; var inputs = {};
for (var input in this._blocks[id].inputs) { for (var input in block.inputs) {
// Ignore blocks prefixed with branch prefix. // Ignore blocks prefixed with branch prefix.
if (input.substring(0, Blocks.BRANCH_INPUT_PREFIX.length) !== if (input.substring(0, Blocks.BRANCH_INPUT_PREFIX.length) !==
Blocks.BRANCH_INPUT_PREFIX) { Blocks.BRANCH_INPUT_PREFIX) {
inputs[input] = this._blocks[id].inputs[input]; inputs[input] = block.inputs[input];
} }
} }
return inputs; return inputs;
@ -123,8 +124,8 @@ Blocks.prototype.getInputs = function (id) {
* @return {!Object} Mutation for the block. * @return {!Object} Mutation for the block.
*/ */
Blocks.prototype.getMutation = function (id) { Blocks.prototype.getMutation = function (id) {
if (typeof this._blocks[id] === 'undefined') return null; var block = this._blocks[id];
return this._blocks[id].mutation; return (typeof block === 'undefined') ? null : block.mutation;
}; };
/** /**
@ -133,8 +134,8 @@ Blocks.prototype.getMutation = function (id) {
* @return {?string} ID of top-level script block. * @return {?string} ID of top-level script block.
*/ */
Blocks.prototype.getTopLevelScript = function (id) { Blocks.prototype.getTopLevelScript = function (id) {
if (typeof this._blocks[id] === 'undefined') return null;
var block = this._blocks[id]; var block = this._blocks[id];
if (typeof block === 'undefined') return null;
while (block.parent !== null) { while (block.parent !== null) {
block = this._blocks[block.parent]; block = this._blocks[block.parent];
} }
@ -271,14 +272,15 @@ Blocks.prototype.createBlock = function (block) {
Blocks.prototype.changeBlock = function (args) { Blocks.prototype.changeBlock = function (args) {
// Validate // Validate
if (args.element !== 'field' && args.element !== 'mutation') return; if (args.element !== 'field' && args.element !== 'mutation') return;
if (typeof this._blocks[args.id] === 'undefined') return; var block = this._blocks[args.id];
if (typeof block === 'undefined') return;
if (args.element === 'field') { if (args.element === 'field') {
// Update block value // Update block value
if (!this._blocks[args.id].fields[args.name]) return; if (!block.fields[args.name]) return;
this._blocks[args.id].fields[args.name].value = args.value; block.fields[args.name].value = args.value;
} else if (args.element === 'mutation') { } else if (args.element === 'mutation') {
this._blocks[args.id].mutation = mutationAdapter(args.value); block.mutation = mutationAdapter(args.value);
} }
}; };