Thanks to visit codestin.com
Credit goes to github.com

Skip to content

some codes can be optimization! #29

Closed
@xgqfrms-GitHub

Description

@xgqfrms-GitHub

https://www.packtpub.com/application-development/learning-javascript-data-structures-and-algorithms

not too good

image

https://github.com/loiane/javascript-datastructures-algorithms/blob/first-edition/chapter06/01-Set.js

https://github.com/loiane/javascript-datastructures-algorithms/blob/master/chapter06/01-Set.js

https://github.com/loiane/javascript-datastructures-algorithms/blob/master/chapter06/01-Set2.js

    this.intersection = function(otherSet){
        var intersectionSet = new Set(); //{1}

        var values = this.values();
        for (var i=0; i<values.length; i++){ //{2}
            if (otherSet.has(values[i])){    //{3}
                intersectionSet.add(values[i]); //{4}
            }
        }

        return intersectionSet;
    };


    this.intersection = function(otherSet){
        let intersectionSet = new Set(); //{1}

        let values = this.values();
        for (let i=0; i<values.length; i++){ //{2}
            if (otherSet.has(values[i])){    //{3}
                intersectionSet.add(values[i]); //{4}
            }
        }

        return intersectionSet;
    };



        intersection(otherSet){
            let intersectionSet = new Set();

            let values = this.values();
            for (let i=0; i<values.length; i++){
                if (otherSet.has(values[i])){
                    intersectionSet.add(values[i]);
                }
            }

            return intersectionSet;
        }

good

image

    this.intersection = (otherSet) => {
        let intersectionSet = new Set();
        // self
        let values = this.values();
        let othervalues = otherSet.values();
        let small_size = ((othervalues.length - values.length) > 0 ? values : othervalues);
        // small & reduce iterate times 
        for (var i = 0; i < small_size.length; i++) {
            if (small_size.has(values[i])) {
                intersectionSet.add(values[i]);
            }
        }
        return intersectionSet;
    };

javascript-datastructures-algorithms

https://github.com/loiane/javascript-datastructures-algorithms

https://github.com/gildata/RAIO/issues/167#issuecomment-330818729

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions