Вопрос

I am having a dilemma as I need to choose the best performing option.

What I have now is a simple array like:

var array = [
   '/index1.html',
   '/index2.html',
   '/index3.html'
];

this array would contain around 60 options only but as I need to separate by language I am thinking of other options such as object literal or JSON format so it would contain all languages and around 1000 options.

var obj = {
            'en' : {
                'title' : 'bla',
                'url':'bla bla bla'                 
            },
            'de' : {
                'title' : 'bla',
                'url':'bla bla bla'                 
            },          
        };

The questions is what do you think would best perform for this? Thank you.

Это было полезно?

Решение

Object literal and JSON are the same thing (correction: see Quentin's comment)

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.

That said, I wouldn't worry about the performance gains here. They're probably negligible.


If you're trying to map your HTML pages to a language, you can either store the URL in a JSON keyed by language abbreviation ('en', 'de', etc.) or you could use a convention for naming your files (index-en.html, index-de.html). It's your choice.

Другие советы

As of 2019, the recommended approach would be a single json string literal loaded into JSON.parse()

See this 2019 Chrome Dev Summit video: Faster apps with JSON.parse.

As long as the JSON string is only evaluated once, the JSON.parse approach is much faster compared to the JavaScript object literal, especially for cold loads. A good rule of thumb is to apply this technique for objects of 10 kB or larger — but as always with performance advice, measure the actual impact before making any changes.

Per the benchmarks, for a large JSON file, the performance gains of JSON.parse over an object literal can be substantial (20% to 70%)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top