Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON vs big JS array

I am making a jQuery autocomplete and I have something between 10~20k registers.

The data is static (i'll run a update script when i need) and I can choose to get the file from a JSON or embed in the page in a single line like:

var title = ["example title 1","example title 2"];

Which one should I choose performance wise? (also I am worried about crashing/lagging peoples browser).. And what about XML?

BTW my PHP script is already using a cache system for the HTML.

like image 757
Checksummmmm Avatar asked Oct 24 '10 19:10

Checksummmmm


People also ask

Which is faster array or JSON?

IF you're searching for a value then JSON can have better performance because it's a hashmap implementation, so the lookup time would be O(1). JSON is also more flexible, since you can have arrays in your JSON, you can have named keys, etc.

Is .JS and .JSON file are the same?

Not exactly. JSON is a data format that is independent of any programming language, although it is derived from JavaScript. The bulk of modern programming languages contain code that can generate and parse JSON data.

Is array faster than object JavaScript?

The short version: Arrays are mostly faster than objects.

Why is object faster than array?

Objects will be used when we need fast access, insertion and removal of an element as objects are comparatively much faster than the arrays in case of insertion and removal.


1 Answers

You should consider using JSON over AJAX to fetch the data. It will make your page seem like it loads a lot faster. You can then use WebWorkers (if the system supports them) to parse the JSON data in a separate thread. This would be idea.

500kb of JSON likely would not cause any significantly significant parsing overhead, so I wouldn't worry about crashing anyone's browser.

like image 134
mattbasta Avatar answered Sep 23 '22 21:09

mattbasta