代码之家  ›  专栏  ›  技术社区  ›  Leo

使用标签数组提高标签算法的性能

  •  -1
  • Leo  · 技术社区  · 7 年前

    这是我的课 Sample .

    A. 样品 实例可以:

    • 有很多标签,比如 Tag1 , Tag2 等
    • 用方法查询 isTagged 找出它是否已被标记(即。 !Tag1 )

        function Sample(){
            // [..]
            this.tags = [];
            // [..]
        }
    
        Sample.prototype.tag = function(tags){
            // [..]
            this.tags[tags] = true;
            // [..]
        };
    
        // if an array is passed, isTagged will return true at the first match ie. not all need to match, just one
        Sample.prototype.isTagged = function(tag){
    
            if(tag){
                if(Array.isArray(tag)){
    
                    let tLength = tag.length;
    
                    while(tLength--){
                        if(isTaggedSingleNoChecks(this, tag[tLength])){
                            return true;
                        }
                    }
    
                    return false;
                }
                else{
                    return isTaggedSingleNoChecks(this, tag);
                }
            }
    
            return false;
        };
    
        function isTaggedSingleNoChecks(sample, tag){
            const isNegated = tag.charAt(0) == "!";
                    
            if(isNegated){
                tag = tag.replace(/^[!]/, "");
    
                return sample.tags[tag]!==true;    
            }
            else{
                return sample.tags[tag]===true; 
            }
        }
        
        // showing usage
        var sample = new Sample();
        sample.tag('Tag1'); 
        sample.tag('Tag2');
        
        console.log(sample.isTagged('Tag1'));
        console.log(sample.isTagged('Tag3'));
        console.log(sample.isTagged('!Tag2'));
        

    这一切都很好,但我的应用程序递归查询 ISTAGGE 成千上万次的 样品 ,我的分析显示这是一个性能瓶颈。

    关于如何提高绩效有什么建议吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   georg    7 年前

    在开始优化之前,先简化代码,去掉最明显的奇怪之处(对象而不是集合、无用的正则表达式等)怎么样

    class Sample {
        constructor() {
            this.tags = new Set();
        }
    
        tag(...tags) {
            for (let t of tags)
                this.tags.add(t);
        }
    
        isTagged(...tags) {
            return tags.some(t =>
                (t[0] === '!')
                    ? !this.tags.has(t.slice(1))
                    : this.tags.has(t)
            )
        }
    }
    

    如果这仍然太慢,则必须求助于全局对象标记反向索引,例如:

    class SetMap extends Map {
        get(key) {
            if (!this.has(key))
                this.set(key, new Set)
            return super.get(key)
        }
    }
    
    let tagIndex = new SetMap()
    
    
    class Sample {
        tag(...tags) {
            for (let t of tags) {
                tagIndex.get(t).add(this)
            }
        }
    
        isTagged(...tags) {
            return tags.some(t => tagIndex.get(t).has(this))
        }
    }
    

    当然,在取消标记(标记移除)方面,尤其是在正确的序列化方面,还需要做更多的工作。

    指数不会立即加速 isTagged 但这将极大地优化查询“查找由X和/或Y标记的对象”。