mirror of
https://github.com/4ian/GDevelop.git
synced 2025-10-15 10:19:04 +00:00
Make access to variables 50% faster
This commit is contained in:
@@ -90,8 +90,9 @@ gdjs.VariablesContainer.prototype.add = function(name, variable) {
|
||||
* @param {string} name Variable to be removed
|
||||
*/
|
||||
gdjs.VariablesContainer.prototype.remove = function(name) {
|
||||
if ( this._variables.containsKey(name) ) {
|
||||
this._variables.get(name).setUndefinedInContainer();
|
||||
var variable = this._variables.items[name];
|
||||
if (variable) {
|
||||
variable.setUndefinedInContainer();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -101,13 +102,11 @@ gdjs.VariablesContainer.prototype.remove = function(name) {
|
||||
* @return {gdjs.Variable} The specified variable. If not found, an empty variable is added to the container.
|
||||
*/
|
||||
gdjs.VariablesContainer.prototype.get = function(name) {
|
||||
var variable = null;
|
||||
if ( !this._variables.containsKey(name) ) { //Add automatically inexisting variables.
|
||||
var variable = this._variables.items[name];
|
||||
if (!variable) { //Add automatically inexisting variables.
|
||||
variable = new gdjs.Variable();
|
||||
this._variables.put(name, variable);
|
||||
}
|
||||
else {
|
||||
variable = this._variables.get(name);
|
||||
} else {
|
||||
if ( variable.isUndefinedInContainer() ) { //Reuse variables removed before.
|
||||
gdjs.Variable.call(variable);
|
||||
}
|
||||
@@ -146,7 +145,8 @@ gdjs.VariablesContainer.prototype.getFromIndex = function(id) {
|
||||
* @return {boolean} true if the variable exists.
|
||||
*/
|
||||
gdjs.VariablesContainer.prototype.has = function(name) {
|
||||
return this._variables.containsKey(name) && !this._variables.get(name).isUndefinedInContainer();
|
||||
var variable = this._variables.items[name];
|
||||
return variable && !variable.isUndefinedInContainer();
|
||||
};
|
||||
|
||||
|
||||
|
16
GDJS/tests/benchmarks/variablescontainer.js
Normal file
16
GDJS/tests/benchmarks/variablescontainer.js
Normal file
@@ -0,0 +1,16 @@
|
||||
describe('gdjs.VariablesContainer', function() {
|
||||
it.only('benchmark convertCoords and convertInverseCoords', function() {
|
||||
this.timeout(20000);
|
||||
var container = new gdjs.VariablesContainer();
|
||||
|
||||
const benchmarkSuite = makeBenchmarkSuite();
|
||||
benchmarkSuite
|
||||
.add('get', () => {
|
||||
container.get('Var1');
|
||||
container.get('Var2');
|
||||
container.get('Var3');
|
||||
});
|
||||
|
||||
console.log(benchmarkSuite.run());
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user