Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some codes can be optimization! #29

Closed
xgqfrms-GitHub opened this issue Sep 20, 2017 · 3 comments
Closed

some codes can be optimization! #29

xgqfrms-GitHub opened this issue Sep 20, 2017 · 3 comments

Comments

@xgqfrms-GitHub
Copy link

xgqfrms-GitHub commented Sep 20, 2017

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

@loiane
Copy link
Owner

loiane commented Sep 30, 2017

Thank you! Will review and update the code!

@loiane
Copy link
Owner

loiane commented Oct 22, 2017

Code updated in third-ed branch

@loiane loiane closed this as completed Oct 30, 2017
@xyzdata
Copy link

xyzdata commented Mar 1, 2018

another version

    this.intersection = (otherSet) => {
        let intersectionSet = new Set(),
            values = this.values(),
            otherValues = otherSet.values(),
            smallSize = ((otherValues.length - values.length) > 0 ? values : otherValues);
        // small & reduce iterate times
        smallSize.forEach((value) => {
            if (smallSize.includes(value)) {
                intersectionSet.add(value);
            }
        });
        return intersectionSet;
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants