Optimize Javascript data Traversal

Source: Internet
Author: User

Optimize Javascript data Traversal
Problem

M is a collection of objects. No object has a unique string type Id.

N is the set of IDs.

Filter out objects whose IDs are not in N from M.

If M has 50 W data records, N may be 0 ~ Arbitrary data.

Solution 1

Use an array to save the set of IDs.

In this way, there are two ways to traverse

1.1

N = [id,...];

For (var m in M)

For (var n in N ){

.....

}

1.2

For (var m in M)

If (N. indexOf (m. id)>-1 ){

}

Solution 2

Use "dict/map" to save the set of IDS

N = {id: 0 },....};

2.1

// Traverse

For (var m in M)

If (n [m. id]! = Undefined ){

....

}

Conclusion

Dict and array PK. The result is obvious. The speed is 2.1> 1.2> 1.1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.