Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #841 from caridy/micro-tunning
Browse files Browse the repository at this point in the history
Micro optimization:

- "delete" is evil in V8, avoid the use of delete on objects.
  • Loading branch information
caridy committed Dec 6, 2012
2 parents 9874480 + ddc4c34 commit b254d13
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 149 deletions.
85 changes: 45 additions & 40 deletions lib/app/addons/ac/composite.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ callback({
buffer = {},
content = {},
meta = {},
sanitizedChildren = {},
name,
perf;

cfg.children = cfg.children || {};
Expand All @@ -220,17 +222,17 @@ callback({
// check to ensure children doesn't have a null child
// in which case it will be automatically discarded to
// facilitate disabling childs based on the context.
Y.Object.each(cfg.children, function(child, name) {
if (!child) {
delete cfg.children[name];
for (name in cfg.children) {
if (cfg.children.hasOwnProperty(name) && cfg.children[name]) {
sanitizedChildren[name] = cfg.children[name];
}
});
}

meta.children = cfg.children;
meta.children = sanitizedChildren;

buffer.__counter__ = Y.Object.size(cfg.children);
buffer.__counter__ = Y.Object.size(sanitizedChildren);

this._dispatchChildren(cfg.children, this.command, buffer,
this._dispatchChildren(sanitizedChildren, this.command, buffer,
function() {
var name;
// Reference the data we want from the "buffer" into our
Expand Down Expand Up @@ -268,44 +270,47 @@ callback({
var childName,
child,
childAdapter,
newCommand;
newCommand,
name;

// Process deferred children before dispatching
Y.Object.each(children, function(child, name) {
// first off, check to see if this child's execution should be
// deferred
if (child.defer) {
// it doesn't make sense to have a deferred child with a
// proxy, because the defer means to proxy it
// through the LazyLoad mojit
if (Y.Lang.isObject(child.proxy)) {
throw new Error('Cannot specify a child mojit spec' +
' with both \'defer\' and \'proxy\'' +
' configurations, because \'defer\'' +
' assumes a \'proxy\' to the LazyLoad' +
' mojit.');
for (name in children) {
if (children.hasOwnProperty(name)) {
child = children[name];
// first off, check to see if this child's execution should be
// deferred
if (child.defer) {
// it doesn't make sense to have a deferred child with a
// proxy, because the defer means to proxy it
// through the LazyLoad mojit
if (Y.Lang.isObject(child.proxy)) {
throw new Error('Cannot specify a child mojit spec' +
' with both \'defer\' and \'proxy\'' +
' configurations, because \'defer\'' +
' assumes a \'proxy\' to the LazyLoad' +
' mojit.');
}
// aha! that means we will give it a proxy to the LazyLoad
// mojit, which will handle lazy execution on the client.
child.proxy = {
type: 'LazyLoad'
};
}
// aha! that means we will give it a proxy to the LazyLoad
// mojit, which will handle lazy execution on the client.
child.proxy = {
type: 'LazyLoad'
};
}
if (Y.Lang.isObject(child.proxy)) {
// found a proxy, replace the child with the proxy and shove
// the child to proxy into it
children[name] = child.proxy;
delete child.proxy;
if (!children[name].config) {
children[name].config = {};
if (Y.Lang.isObject(child.proxy)) {
// found a proxy, replace the child with the proxy and shove
// the child to proxy into it
children[name] = child.proxy;
if (!children[name].config) {
children[name].config = {};
}
// remove any defer or proxy flags so it doesn't reload
// infinitely
child.proxy = undefined;
child.defer = false;
children[name].config.proxied = child;
}
// remove any defer or proxy flags so it doesn't reload
// infinitely
child.proxy = undefined;
child.defer = false;
children[name].config.proxied = child;
}
});
}

for (childName in children) {
if (children.hasOwnProperty(childName)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/app/addons/ac/deploy.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ YUI.add('mojito-deploy-addon', function(Y, NAME) {
// yui.config goes through a different channel (yuiConfig),
// so we should remove it from the appConfigClient.
if (appConfigClient.yui.config) {
delete appConfigClient.yui.config;
appConfigClient.yui.config = undefined;
}


Expand Down
4 changes: 2 additions & 2 deletions lib/app/addons/ac/partial.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ YUI.add('mojito-partial-addon', function(Y, NAME) {
this.data += data;

// Remove whatever "content-type" was set
delete meta.http.headers['content-type'];
meta.http.headers['content-type'] = undefined;

// Remove whatever "view" was set
delete meta.view;
meta.view = undefined;

if (!more) {
cb(null, this.data, this.meta);
Expand Down
15 changes: 10 additions & 5 deletions lib/app/addons/ac/url.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
YUI.add('mojito-url-addon', function(Y, NAME) {

function objectToQueryStr(obj, removeEmpty) {
var sanitized = {},
key;
// If "removeEmpty" is true we remove any params with no value.
if (removeEmpty) {
Y.Object.each(obj, function(val, key) {
if (!val) {
delete obj[key];
// by creating a copy with valid values we gain
// a little bit with V8
for (key in obj) {
if (obj.hasOwnProperty(key) && obj[key]) {
sanitized[key] = obj[key];
}
});
}
obj = sanitized;
}

if (Y.Lang.isObject(obj) && Y.Object.size(obj) > 0) {
Expand Down Expand Up @@ -66,7 +71,7 @@ YUI.add('mojito-url-addon', function(Y, NAME) {
var url,
query = base + '.' + action;

routeParams = objectToQueryStr(routeParams);
routeParams = objectToQueryStr(routeParams || {});

if (routeParams && routeParams.length) {
query = query + '?' + routeParams;
Expand Down
8 changes: 4 additions & 4 deletions lib/app/addons/rs/yui.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
// applying some extra optimizations
module.langPack = lang || '*';
module.intl = true;
delete module.expanded_map;
module.expanded_map = undefined;
}

if (module.condition && module.condition.test) {
Expand All @@ -832,7 +832,7 @@ YUI.add('addon-rs-yui', function(Y, NAME) {

expanded_modules[module.name] = module;
for (i = 0; i < MODULE_META_PRIVATE_ENTRIES.length; i += 1) {
delete module[MODULE_META_PRIVATE_ENTRIES[i]];
module[MODULE_META_PRIVATE_ENTRIES[i]] = undefined;
}
}
}
Expand Down Expand Up @@ -910,14 +910,14 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
// We need to clear YUI's cached dependencies, since there's no
// guarantee that the previously calculated dependencies have been done
// using the same context as this calculation.
delete YUI.Env._renderedMods;
YUI.Env._renderedMods = undefined;

// Trick the loader into thinking it's -not- running on nodejs.
// This is the official way to do it.
originalYUAnodejs = Y.UA.nodejs;
Y.UA.nodejs = ('server' === env);

// Use ignoreRegistered here instead of the old `delete YUI.Env._renderedMods` hack
// Use ignoreRegistered here instead of the old `YUI.Env._renderedMods = undefined;` hack
loader = new Y.Loader({ ignoreRegistered: true });
// Only override the default if it's required
if (this.yuiConfig.base) {
Expand Down
2 changes: 1 addition & 1 deletion lib/app/addons/view-engines/mu.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ YUI.add('mojito-mu', function(Y, NAME) {
CACHE[tmpl] = obj.responseText;

queue = QUEUE_POOL[tmpl].slice(0);
delete QUEUE_POOL[tmpl];
QUEUE_POOL[tmpl] = undefined;

for (i = 0, iC = queue.length; i < iC; i += 1) {
myAdapter = queue[i].adapter;
Expand Down
18 changes: 9 additions & 9 deletions lib/app/autoload/action-context.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


/*jslint anon:true, nomen:true*/
/*global YUI*/
/*global YUI, setTimeout, clearTimeout*/


/**
Expand Down Expand Up @@ -137,7 +137,7 @@ YUI.add('mojito-action-context', function(Y, NAME) {
// other execution environments. For example, the client runtime
// does not need to have the parameters of the mojits that were used
// to construct the initial client DOM.
delete children[k].params;
children[k].params = undefined;
});
return children;
}
Expand Down Expand Up @@ -204,7 +204,7 @@ YUI.add('mojito-action-context', function(Y, NAME) {
}

return ((wraptag) ? '<' + wraptag + '>' : '') + js +
((wraptag) ? '</' + wraptag + '>' : ''
((wraptag) ? '</' + wraptag + '>' : ''
);
}

Expand Down Expand Up @@ -351,8 +351,8 @@ YUI.add('mojito-action-context', function(Y, NAME) {
', controller: ' + my.instance.controller +
', action: ' + actionFunction;

// Clear the timer reference so our invocation of error()
// doesn't try to clear it.
// Clear the timer reference so our invocation of error()
// doesn't try to clear it.
my._timer = null;

// Create an HTTP Timeout error with controller/action info.
Expand Down Expand Up @@ -389,7 +389,7 @@ YUI.add('mojito-action-context', function(Y, NAME) {
*/
done: function(data, meta, more) {

// If we have an active timer clear it immediately.
// If we have an active timer clear it immediately.
if (this._timer) {
clearTimeout(this._timer);
this._timer = null;
Expand Down Expand Up @@ -443,9 +443,9 @@ YUI.add('mojito-action-context', function(Y, NAME) {
if (meta.serialize && serializer[meta.serialize]) {
// Warning: this metod can change the "meta" object
data = serializer[meta.serialize].apply(this, [data, meta]);
// Once we are done remove the "serialize" option so others don't
// Once we are done, invalidate the "serialize" option so others don't
// use it by mistake
delete meta.serialize;
meta.serialize = undefined;
}

// We want to know the view name, id, and binder used later so make sure
Expand Down Expand Up @@ -602,7 +602,7 @@ YUI.add('mojito-action-context', function(Y, NAME) {
* "This does not exist in my app".
*/
error: function(err) {
// If we have an active timer clear it immediately.
// If we have an active timer clear it immediately.
if (this._timer) {
clearTimeout(this._timer);
this._timer = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/app/autoload/dispatch.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ YUI.add('mojito-dispatcher', function (Y, NAME) {

// this prevents the server from trying to RPC itself
// FUTURE: might be a better place to do this
delete command.rpc;
command.rpc = false;

Y.log('Dispatching instance "' + (command.instance.base || '@' +
command.instance.type) + '" through RPC tunnel.', 'info', NAME);
Expand Down
4 changes: 2 additions & 2 deletions lib/app/autoload/mojit-proxy.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ YUI.add('mojito-mojit-proxy', function(Y, NAME) {

if (child) {
if (this.config.children) {
delete this.config.children[id];
this.config.children[id] = undefined;
}
delete children[id];
children[id] = undefined;
}
},

Expand Down
5 changes: 5 additions & 0 deletions lib/app/autoload/mojito-client.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,11 @@ YUI.add('mojito-client', function(Y, NAME) {
// unbindNode(mojits[viewId].proxy._binder,
// mojits[viewId].handles);
mojits[viewId].proxy._destroy(retainNode);
// is there a better alternative for this delete?
// maybe not, but it might introduce a perf penalty
// if a lot of mojits are created and destroyed,
// and we can't use the undefined trick because
// viewId has an infinite domain
delete mojits[viewId];

// We don't manage binder children automatically, but any time a
Expand Down
2 changes: 1 addition & 1 deletion lib/app/autoload/perf.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ YUI.add('mojito-perf', function (Y, NAME) {
}
}
}
delete buffer[group];
}
}
buffer = {};
// dumping to disk
if (config.logFile) {
Y.log('Dumping performance metrics into disk: ' +
Expand Down
21 changes: 14 additions & 7 deletions lib/app/autoload/route-maker.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ YUI.add('mojito-route-maker', function(Y, NAME) {
var parts = query.split('?'),
call = parts[0],
params = {},
residual = {},
route,
uri;
uri,
k;

// TODO: don't assign to a parameter.
verb = verb || 'GET';
Expand All @@ -244,13 +246,18 @@ YUI.add('mojito-route-maker', function(Y, NAME) {

uri = route.path;

Y.Object.each(route.query, function(v, k) {
uri = uri.replace(':' + k, v);
delete params[k];
});
for (k in params) {
if (params.hasOwnProperty(k)) {
if (route.query && route.query[k]) {
uri = uri.replace(':' + k, params[k]);
} else {
residual[k] = params[k];
}
}
}

if (!Y.Object.isEmpty(params)) {
uri += '?' + Y.QueryString.stringify(params);
if (!Y.Object.isEmpty(residual)) {
uri += '?' + Y.QueryString.stringify(residual);
}

return uri;
Expand Down
2 changes: 1 addition & 1 deletion lib/app/autoload/store.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ YUI.add('mojito-client-store', function(Y, NAME) {
// looping over it again. (User-provided callbacks can take a long
// time to run, and while they are more callbacks can get queued.)
q = QUEUED[url].splice(0, QUEUED[url].length);
delete QUEUED[url];
QUEUED[url] = undefined;
for (i = 0; i < q.length; i += 1) {
// We need to give each receiver a separate copy, since the
// returned data is an -object-, and changes in one will bleed
Expand Down
Loading

0 comments on commit b254d13

Please sign in to comment.