yuku-t/jquery-textcomplete — pull request #2 by ykzts (2013-09-04 22:42:03+00:00)
Title: Refactor to a utility function for lock

```javascript
(function() {
'use strict';

var toArray = function () {
return Array.prototype.concat.apply([], arguments);
};

var lock1 = (function () {
var table, i;
table = {};
i = 0;
return function (func) {
var id;
id = i;
i += 1;
return function () {
var args, free;
if (table[id]) return;
table[id] = true;
free = function () { table[id] = false; };
args = [free];
func.apply(this, args.concat(toArray(arguments)));
};
};
})();

var lock2 = (function () {
var list;
list = [];
return function (func) {
var free;
free = function () {
var index;
index = list.indexOf(func);
index >= 0 && list.splice(index, 1);
}
return function () {
var args;
if (list.indexOf(func) >= 0) return;
list.push(func);
args = toArray(arguments);
args.unshift(free);
func.apply(this, args);
};
};
})();

var i;

i = 0;
console.time('lock1');
while (++i < 10000) {
lock1(function (free, query) {
free();
})({});
}
console.timeEnd('lock1'); // => lock1: 60.060ms

i = 0;
console.time('lock2');
while (++i < 10000) {
lock2(function (free, query) {
free();
})({});
}
console.timeEnd('lock2'); // => lock2: 19.290ms
})();
```

300% faster.