ewein
ewein

Reputation: 2735

Javascript Data Structures for Efficient Insertion and Searching

I am in need of a Javascript data structure which will allow me to insert strings and search for strings efficiently. I have been looking around and the only data structures I have come across are objects and arrays. Objects are more used for encapsulation and cannot really be used for searching and using arrays can be slow. Are there any other data structures that will allow me to insert and search strings efficiently? Right now at best I could do a binary search on an array. Any other ideas? Thanks.

Upvotes: 3

Views: 2562

Answers (2)

Nash Worth
Nash Worth

Reputation: 2574

Objects are more used for encapsulation and cannot really be used for searching

That was true in classical languages, not so true in JS.

   var obj = { memberone: "value1" }

   var value = obj["memberone"];

   //value === "value1"

Objects can be searched in JS. Bear with me...

and using arrays can be slow.

Yes, can be - but don't have to be.

Are there any other data structures that will allow me to insert and search strings efficiently?

Data structures? No. Again that is a classical perspective. In JS, it is different.

Check out _underscore.js.

  1. It is 4k min gzip.
  2. It provides a number of advanced iterator helpers (so you don't have to)
  3. It provides templates to display your data to screen efficently.
  4. It will benefit the rest of your development, maintenance, and implementations.

This is a good example of JS flexibility.

Hope that helps. All the best! Nash

Upvotes: 3

Umesh Aawte
Umesh Aawte

Reputation: 4690

There are some more I found after some goggling,

Javascript data structures - a collection object

One more thing you can use json objects and its JavaScript API to manipulate same. Please refer same here

Upvotes: 1

Related Questions