Mottie/Keyboard

Possible performance issue on slow systems

Closed this issue · 1 comments

In base.init one of the actions is deep copying of options which is done like this:

base.options = o = $.extend(true, {}, $keyboard.defaultOptions, options);

While it seems to be a cheap operation it could be quite the opposite. Imagine that someone decided to position a keyboard relative to the window. In this case options would be like this:

$('a').keyboard({
  'position' = {
    'at': 'center bottom',
    'my': 'center bottom',
    'of': window  // <= this is an object!
  }
});

In this case .extend will be copying every single property of window which, as we know, is a scope of global variables. The problem becomes even more bad when you use closure compiler: making optimizations it moves local variables into global scope.

Of course, there are workarounds but in most cases people don't realize the problem exists until their project is ran on a slow system. I'd proporse to extract position from options before making deep copy and then copy it separately using shallow copy approach.

Hi @ihsoft!

That is interesting. I've never thought about extend being that slow.

I would think extend would make a reference to window and not clone the entire object. I wonder if changing it to $(window) would speed things up?

Also, I would not think there would be an issue with closure compiler causing issues if the above code is wrapped in a document ready function or a self-executing anonymous function.

Either way, it wouldn't be hard to work around this issue in the code, specifically for the position.of definition.