speed up string functions

This commit is contained in:
John Alanbrook 2024-07-25 16:14:37 -05:00
parent 0a7f5a5cdd
commit 6047452b62
8 changed files with 62 additions and 79 deletions

View file

@ -409,7 +409,7 @@ Object.dainty_assign = function(target, source)
Object.isObject = function(o)
{
return (typeof o === 'object' && !Array.isArray(o));
return (o instanceof Object && !(o instanceof Array));
}
Object.setter_assign = function(target, source)
@ -860,25 +860,6 @@ Object.defineProperty(String.prototype, 'trimchr', {
}
});
Object.defineProperty(String.prototype, 'startswith', {
value: function(val) {
if (!val) return false;
return this.startsWith(val);
}
});
Object.defineProperty(String.prototype, 'endswith', {
value: function(val) {
if (!val) return false;
return this.endsWith(val);
}
});
Object.defineProperty(String.prototype, 'pct', {
value: function(val) {
}
});
Object.defineProperty(String.prototype, 'uc', { value: function() { return this.toUpperCase(); } });
Object.defineProperty(String.prototype, 'lc', {value:function() { return this.toLowerCase(); }});
@ -900,6 +881,18 @@ Object.defineProperty(Array.prototype, 'copy', {
}
});
Object.defineProperty(Array.prototype, 'forFrom', {
value: function(n, fn) {
for (var i = n; i < this.length; i++) fn(this[i]);
}
});
Object.defineProperty(Array.prototype, 'forTo', {
value: function(n, fn) {
for (var i = 0; i < n; i++) fn(this[i]);
}
});
Object.defineProperty(Array.prototype, 'dofilter', {
value: function(fn) {
for (let i = 0; i < this.length; i++) {
@ -1033,19 +1026,6 @@ value: function(b) {
return c;
}});
/*Object.defineProperty(Array.prototype, 'concat', {
value: function(b) {
var result = [];
for (var i = 0; i < this.length; i++)
result.push(this[i]);
for (var i = 0; i < b.length; i++)
result.push(b[i]);
return result;
}
});*/
Object.defineProperty(Array.prototype, 'normalized', {
value: function() {
var c = this.slice();

View file

@ -79,9 +79,7 @@ var sprite = {
return;
}
if (p === this.path) return;
sprite_rmbucket(this);
this._p = p;
sprite_addbucket(this);
this.del_anim?.();
this.texture = game.texture(p);
@ -101,7 +99,6 @@ var sprite = {
return this._p;
},
kill() {
sprite_rmbucket(this);
this.del_anim?.();
this.anim = undefined;
this.gameobject = undefined;
@ -175,7 +172,6 @@ component.sprite = function(obj) {
sp.transform = obj.transform;
sp.guid = prosperon.guid();
allsprites[sp.guid] = sp;
sprite_addbucket(sp);
if (component.sprite.make_hook) component.sprite.make_hook(sp);
return sp;
}

View file

@ -569,7 +569,7 @@ var editor = {
obj.ur = sub;
return;
} else if (!sub.startswith(obj.ur)) {
} else if (!sub.startsWith(obj.ur)) {
console.warn(`Cannot make an ur of type ${sub} from an object with the ur ${obj.ur}`);
return;
}
@ -1433,7 +1433,7 @@ replpanel.inputs.tab = function() {
var stub = this.value.fromlast('.');
var replobj = (editor.selectlist.length === 1) ? "editor.selectlist[0]" : "editor.edit_level";
if (this.value.startswith("this."))
if (this.value.startsWith("this."))
keyobj = keyobj.replace("this", replobj);
if (!this.value.includes('.')) keys.push("this");
@ -1461,7 +1461,7 @@ replpanel.inputs.tab = function() {
if (stub)
this.value = o + '.' + comp;
else if (this.value.endswith('.'))
else if (this.value.endsWith('.'))
this.value = o + '.' + comp;
else
this.value = comp;
@ -1677,7 +1677,7 @@ var openlevelpanel = Object.copy(inputpanel, {
keycb() {
if(this.value)
this.assets = this.allassets.filter(x => x.startswith(this.value));
this.assets = this.allassets.filter(x => x.startsWith(this.value));
else
this.assets = this.allassets.slice();
for (var m in this.mumlist)

View file

@ -25,6 +25,14 @@ Object.defineProperty(String.prototype, "folder", {
globalThis.Resources = {};
Resources.rm_fn = function(fnstr, text)
{
while (text.match(fnstr)) {
}
}
Resources.rm_fn.doc = "Remove calls to a given function from a given text script.";
Resources.replpath = function (str, path) {
if (!str) return str;
if (str[0] === "/") return str.rm(0);
@ -49,6 +57,10 @@ Resources.replstrs = function (path) {
var regexp = /"[^"\s]*?\.[^"\s]+?"/g;
var stem = path.dir();
// remove console statements
//script = script.replace(/console\.(.*?)\(.*?\)/g, '');
//script = script.replace(/assert\(.*?\)/g, '');
script = script.replace(regexp, function (str) {
var newstr = Resources.replpath(str.trimchr('"'), path);
return `"${newstr}"`;
@ -222,7 +234,7 @@ global.check_registers = function (obj) {
}
for (var k in obj) {
if (!k.startswith("on_")) continue;
if (!k.startsWith("on_")) continue;
var signal = k.fromfirst("on_");
Event.observe(signal, obj, obj[k]);
}
@ -728,12 +740,12 @@ var Register = {
var fns = [];
n.register = function (fn, oname) {
if (typeof fn !== "function") return;
if (!(fn instanceof Function)) return;
var dofn = function(...args) {
var st = profile.now();
fn(...args);
profile.addreport(name, oname, st);
profile.addreport(name, oname, st);
}
fns.push(dofn);

View file

@ -61,8 +61,8 @@ var entity = {
},
sync() {
this.components.forEach(function(x) { x.sync?.(); });
this.objects.forEach(function(x) { x.sync(); });
for (var c of Object.values(this.components)) c.sync?.();
for (var o of Object.values(this.objects)) o.sync();
},
delay(fn, seconds) {
@ -150,7 +150,7 @@ var entity = {
if (!text)
ent.ur = emptyur;
else if (typeof text === 'object' && text) {// assume it's an ur
else if (text instanceof Object) {// assume it's an ur
ent.ur = text;
text = ent.ur.text;
config = [ent.ur.data, config].filter(x => x).flat();
@ -164,19 +164,19 @@ var entity = {
if (typeof text === 'string')
use(text, ent);
else if (Array.isArray(text))
text.forEach(path => use(path,ent));
for (var path of text) use(path,ent);
if (typeof config === 'string')
Object.merge(ent, json.decode(Resources.replstrs(config)));
else if (Array.isArray(config))
config.forEach(function(path) {
for (var path of config) {
if (typeof path === 'string') {
console.info(`ingesting ${path} ...`);
Object.merge(ent, json.decode(Resources.replstrs(path)));
}
else if (typeof path === 'object')
else if (path instanceof Object)
Object.merge(ent,path);
});
};
ent.reparent(this);
@ -191,9 +191,9 @@ var entity = {
check_registers(ent);
if (typeof ent.load === 'function') ent.load();
if (ent.load instanceof Function) ent.load();
if (sim.playing())
if (typeof ent.start === 'function') ent.start();
if (ent.start instanceof Function) ent.start();
Object.hide(ent, 'ur', 'components', 'objects', 'timers', 'guid', 'master');
@ -235,8 +235,8 @@ var entity = {
return ent;
},
disable() { this.components.forEach(function(x) { x.disable(); }); },
enable() { this.components.forEach(function(x) { x.enable(); }); },
disable() { for (var x of this.components) x.disable(); },
enable() { for (var x of this.components) x.enable(); },
this2screen(pos) { return game.camera.world2view(this.this2world(pos)); },
screen2this(pos) { return this.world2this(game.camera.view2world(pos)); },
@ -280,7 +280,7 @@ var entity = {
var bb = boxes.shift();
boxes.forEach(function(x) { bb = bbox.expand(bb, x); });
for (var x of boxes) bb = bbox.expand(bb, x);
bb = bbox.move(bb, this.pos);
@ -338,7 +338,7 @@ dup(diff) {
this.__kill = true;
console.spam(`Killing entity of type ${this.ur}`);
this.timers.forEach(t => t());
for (var t of this.timers) t();
this.timers = [];
Event.rm_obj(this);
input.do_uncontrol(this);
@ -358,13 +358,12 @@ dup(diff) {
delete this.components;
this.clear();
if (typeof this.stop === 'function') this.stop();
if (this.stop instanceof Function) this.stop();
game.tag_clear_guid(this.guid);
for (var i in this) {
if (typeof this[i] === 'object') delete this[i];
if (typeof this[i] === 'function') delete this[i];
if (this[i] instanceof Object || this[i] instanceof Function) delete this[i];
}
},
@ -482,17 +481,17 @@ var gameobject = {
var newpos = relative.this2world(x);
var move = newpos.sub(this.pos);
this.rpos = newpos;
this.objects.forEach(x => x.move(move));
for (var o of this.objects) o.move(move);
},
set_angle(x, relative = world) {
var newangle = relative.angle + x;
var diff = newangle - this.angle;
this.rangle = newangle;
this.objects.forEach(obj => {
for (var obj of this.objects) {
obj.rotate(diff);
obj.set_pos(Vector.rotate(obj.get_pos(obj.master), diff), obj.master);
});
}
},
set_scale(x, relative = world) {
@ -500,10 +499,10 @@ var gameobject = {
var newscale = relative.scale.map((s,i) => x[i]*s);
var pct = this.scale.map((s,i) => newscale[i]/s);
this.rscale = newscale;
this.objects.forEach(obj => {
for (var obj of this.objects) {
obj.grow(pct);
obj.set_pos(obj.get_pos(obj.master).map((x,i) => x*pct[i]), obj.master);
});
};
},
get_pos(relative = world) {
@ -650,17 +649,17 @@ function apply_ur(u, ent) {
if (typeof text === 'string')
use(text, ent);
else if (Array.isArray(text))
text.forEach(path => use(path,ent));
for (var path of text) use(path,ent);
if (typeof data === 'string')
Object.merge(ent, json.decode(Resources.replstrs(data)));
else if (Array.isArray(data)) {
data.forEach(function(path) {
for (var path of data) {
if (typeof path === 'string')
Object.merge(ent, json.decode(Resources.replstrs(data)));
else if (typeof path === 'object')
else if (path instanceof Object)
Object.merge(ent,path);
});
};
}
}
}

View file

@ -126,7 +126,7 @@ profile.endframe = function()
var print_frame = function(frame, indent, title)
{
var avg = frame._times.reduce((sum, e) => sum += e)/frame._times.length;
say(indent + `${title} ::::: ${profile.best_t(avg)}`);
say(indent + `${title} ::::: ${profile.best_t(avg)} (${frame._times.length} hits)`);
for (var i in frame) {
if (i === '_times') continue;

View file

@ -660,11 +660,7 @@ render.box = function(pos, wh, color = Color.white) {
check_flush(render.flush_poly);
};
render.window = function(pos, wh, color) {
var p = pos.slice();
p = p.add(wh.scale(0.5));
render.box(p,wh,color);
};
render.window = function(pos, wh, color) { render.box(pos.add(wh.scale(0.5)),wh,color); };
render.text_bb = function(str, size = 1, wrap = -1, pos = [0,0])
{

View file

@ -171,7 +171,7 @@ var listpanel = Object.copy(inputpanel, {
keycb() {
if(this.value)
this.assets = this.allassets.filter(x => x.startswith(this.value));
this.assets = this.allassets.filter(x => x.startsWith(this.value));
else
this.assets = this.allassets.slice();
for (var m in this.mumlist)