使用JavaScript跟踪对象键的有效方法
作者:互联网
我正在使用带有陷阱的Proxy对象来跟踪对象键,以便我可以轻松地迭代和/或从对象中选择一个随机密钥,而性能开销很小.目前,我在添加时将密钥存储在数组中.这对于插入和随机选择非常有效,但是当删除属性时,开销很大:
// Benchmark
var testObject = createProxy();
var start = performance.now();
for( var i = 0; i < 1e4; i++ )
testObject[Math.random() * 1e6 << 0] = true;
for( var i in testObject )
if( i[0] !== '_' )
delete testObject[ i ];
var end = performance.now();
var total = ( end - start );
console.log( 'Test took ' + total + ' ms' );
// Implementation
function createProxy() {
function keyTracker() {
const set = new Set();
function defineProperty( target, property, descriptor ) {
target[property] = descriptor.value;
if( property[0] === '_' ) return true;
if( set.has( property ) ) return true;
set.add( property );
target[ '__keys' ].push( property );
return true;
}
function deleteProperty( target, property ) {
if( property[ 0 ] === '_' ) return true;
delete target[ property ];
if( !set.delete( property ) ) return true;
target[ '__keys' ] = target[ '__keys' ].filter(
key => key !== property
);
return true;
}
return { defineProperty, deleteProperty };
}
var proxy = new Proxy(
Object.defineProperty( {}, '__keys', {
configurable: true,
enumerable: false,
writable: true,
value: []
} ), keyTracker() );
return proxy;
}
随着对象中键的数量的增加,调用Array.filter()会成倍增加.我正在寻找一种解决方案,可以让我避免必须调用它来删除单个元素.
有没有办法我可以重新设计这个以允许O(1)插入,随机选择和删除键?
解决方法:
你可以使用排序数组,然后使用二进制搜索来实现O(log n)
// Benchmark
Array.prototype.binarySearch = function (target, comparator) {
var l = 0,
h = this.length - 1,
m, comparison;
comparator = comparator || function (a, b) {
return (a < b ? -1 : (a > b ? 1 : 0)); /* default comparison method if one was not provided */
};
while (l <= h) {
m = (l + h) >>> 1; /* equivalent to Math.floor((l + h) / 2) but faster */
comparison = comparator(this[m], target);
if (comparison < 0) {
l = m + 1;
} else if (comparison > 0) {
h = m - 1;
} else {
return m;
}
}
return~l;
};
Array.prototype.binaryInsert = function (target, duplicate, comparator) {
var i = this.binarySearch(target, comparator);
if (i >= 0) { /* if the binarySearch return value was zero or positive, a matching object was found */
if (!duplicate) {
return i;
}
} else { /* if the return value was negative, the bitwise complement of the return value is the correct index for this object */
i = ~i;
}
this.splice(i, 0, target);
return i;
};
Array.prototype.binaryDelete = function (target, duplicate, comparator) {
var i = this.binarySearch(target, comparator);
if (i >= 0) { /* if the binarySearch return value was zero or positive, a matching object was found */
this.slice(i,1)
}
return i;
};
var testObject = createProxy();
var start = performance.now();
for( var i = 0; i < 1e4; i++ )
testObject[Math.random() * 1e6 << 0] = true;
for( var i in testObject )
if( i[0] !== '_' )
delete testObject[ i ];
var end = performance.now();
var total = ( end - start );
console.log( 'Test took ' + total + ' ms' );
// Implementation
function createProxy() {
function keyTracker() {
const set = new Set();
function defineProperty( target, property, descriptor ) {
target[property] = descriptor.value;
if( property[0] === '_' ) return true;
if( set.has( property ) ) return true;
set.add( property );
target[ '__keys' ].binaryInsert( property );
return true;
}
function deleteProperty( target, property ) {
if( property[ 0 ] === '_' ) return true;
delete target[ property ];
if( !set.delete( property ) ) return true;
target[ '__keys' ].binaryDelete(property)
return true;
}
return { defineProperty, deleteProperty };
}
var proxy = new Proxy(
Object.defineProperty( {}, '__keys', {
configurable: true,
enumerable: false,
writable: true,
value: []
} ), keyTracker() );
return proxy;
}
从这里使用二进制搜索植入
javascript-binary-search
标签:dynamic-proxy,javascript 来源: https://codeday.me/bug/20190828/1746568.html