Javascript Data Structures for Efficient Insertion and Searching - javascript

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.

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.
It is 4k min gzip.
It provides a number of advanced iterator helpers (so you don't have to)
It provides templates to display your data to screen efficently.
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

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

Related

Does sorting JSON keys/attributes alphabetically make a difference to performance

If I have a JSON file that let's say represents a dictionary of words, where the word is the key and the value is the definition of that word. Would sorting and organizing the keys in alphabetical order in the JSON file make a difference to the performance when searching a word to find its definition (plus maybe other details about that word) in JS - that is if there were thousands of words or even more?
Or does JSON and Javascript already have an algorithm built in to find results optimally without the need to sort the data for better performance?
Also, I wouldn't mind having an alternative data structure or format or library that could give faster results for this kind of search problem suggested to me! (but of course, this suggestion isn't part of the main question)
There is no need to sort the keys in and json structure. The reading of a key/value will not be faster after sorting. See the discussion here
If you have very big structures, you can implement a TRIE by yourself. See: Wiki TRIE. Or see this: trie.js
Sorting the keys does not impact the search performance. Javascript objects may not remember the order of insertion
If possible, use Map object - The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

What is the right/preferred name for a data structure akin to JavaScript objects in Java or any other language?

I have been reminded several times (and now remind SO users myself) that JSON is a text encoding, and is different from JavaScript Objects.
In PHP, you can work with arrays in much the same way as in JavaScript. In J2EE you can create a JsonArray or a JsonStructure (https://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html)... In reference to these Java objects, to me this already seem like a bastardization of what JSON was created to mean.
So my question: What is the correct way to describe a JSON-like data structure in a language other than JavaScript? Is there anything better than JSON? If I said to my colleague asking about the contents of an Array in my PHP page, is it wrong to say "It is a JSON"? The alternative I guess is associative array (or map) but an associative array is much broader than a JavaScript object, which again can only contain arrays, objects, and key-value pairs.
JSON is an encoded representation of your data structures in PHP or whatever language you choose to use.
This process can be referred to by many names, serialization, marshalling to name a few.
I am happy with Hovercraft Full of Eels answer. While "JSON" might not have been intended to be used for this purpose, it is the best way to describe this kind of data structure.

Hashtable vs objects In javascript

I’m new to data structure and I’m learning it in Javascript.
My Question is:
Why do we need hash tables when we 've objects in javascript?
Can anybody give me a situation where hash tables will be more useful than objects?
"Hashtable" is called different things in different languages. Java has Hashtable and HashMap, Ruby has Hash, Python has dict... in JavaScript, it's called Map.
Objects' keys are limited to strings; Map keys can be anything.
Objects support inheritance; a Map only contains what is specifically put into it.
Think you means Map instead of HashTable. IMHO Map may be more useful and perform better if you need one of that:
keep order of insertions of key/value pairs;
frequent additional and removal;
key which not String/Symbol.
I think you can obtain more information at MDN
The MDN docs on this are quite helpful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Objects_and_maps_compared
Most notably, using a map gives you the advantage of using anything as a key, maps retain order, and may perform better when constantly adding and removing values.

What mathematical tool is similar to a JavaScript object?

I am trying to write documentation on a piece of Javascript code but I am having trouble describing the objects made by the code in a concise and understandable way. It is especially difficult because the objects have nested objects (often multiple layers).
Is there any mathematics that involves things with keys and attached values?
If not, how best can I describe an object with multiple nest objects in a concise manner?
Note: Just showing an example of an object is not enough as the structure changes often. Also, there are mathematical relationships between the keys and the values (coupon dates as keys and coupon payments as values).
I would say that Javascript objects are functions or mappings, in that they map keys to values.
Beyond that, it is hard to compare... the domain can encompass numbers, and a subset of all strings. As simple as that is to say, I'm not sure what mathematical field (etc) the domain would be equivalent to!
The range would, of course, be worse, as values in the range can be numbers, strings, booleans, undefined, further objects, or functions. However, I think the concept of an object being a mapping is fairly intuitive.
This doesn't include the prototype style inheritance, but I'm not sure how deep you want to go...
I saw a comment on it earlier, JavaScript objects pretty much follow the associative array abstract data type, which is a mathematical concept by virtue since computer science is basically a subset of applied mathematics, but if you need a true mathematical representation there's relational algebra which was created for relational databases (close enough) and is essentially an extension of set theory... just remember math doesn't necessarily mean it's clear and concise – Patrick Barr yesterday

What else can you call a JSON object - is it hash map? - conceptual questions

One thing bothered me quite a bit when learning Javascript while trying to incorporate CS concepts - what exactly is JSON? I heard of people calling it associative arrays, key value pair objects, Javascript Objects.
Correct me if I'm wrong - I know in Java Hash Maps are more or less key value pairs. Since I feel like JSON shares a lot of properties with that, can I safely call JSON objects hash maps without sounding like an idiot in interviews?
Thanks
No. You could call it a lightweight human readable structured text document. Human readable like xml, but not as verbose hence lightweight. You might compare it to other data encoding mechanisms like protocol buffers, yaml or (the previously mentioned) XML.
JSON - JavaScript Object Notation
Data objects consisting of attribute-value pairs.

Categories

Resources