-4

If I want to store multiple values related to each other, should I use multiple variables or a single object? Which one would be more efficient?
Note: These variables are defined inside an IIFE, so they are not globals.

Example:

// Option 1
let value1 = "Value 1";
let value2 = 2;
let value3 = true;

// Option 2
let value = {
    "1": "Value 1",
    "2": 2,
    "3": true
};

// Which one is more efficient?
Bill
  • 553
  • 1
  • 7
  • 19

2 Answers2

1

EDIT: OP actually put these variables inside an immediately invoked function and a function is already an object, therefore just keep them there.

Original answer:

In short, Objects.

Are you defining your variables as globals? Global variables clutter up the global namespace and are slower to look up than local variables.

Since you used let, there is no variable hoisting and your variables have block scope, tending to be much tidier. Any context of execution different than Array and global (except local blocks) is actually an Object. So depending on the context of execution, in a tidy context of execution like a function, a closure, an instance of a "class" (ES6, or functions used as classes), you would already have your desired tidyness. Functions and classes, extending Object are therefore Objects, inheriting Object.prototype.

Object.prototype is useful when handling data, for instance Object.assign() is useful for shallow coying and merging.

You can easily access the Array.prototype by using Object.keys() and Object.values() , allowing many operations such as map, filter, reduce. Here is an example:

Object.filter = (obj, predicate) => Object.keys(obj)
    .filter( key => predicate(obj[key]))
    .reduce( (res, key) => (res[key] = obj[key], res), {} );

So generally speaking Object is a better idea resulting in wider possibilities. Of course you could create Objects when you need them as well.

To sum up your 3 choices are Array, Object (including functions, classes, (nevermind blocks)) or Global. Globals are to be avoided, Objects may dominate Arrays.

Attersson
  • 4,755
  • 1
  • 15
  • 29
  • I am defining those variables inside an IIFE, so they are not globals. – Bill May 14 '18 at 01:06
  • A function, including IIFE is an Object. Therefore your question is actually IIFE vs "vanilla" Object and you already need an IIFE so just keep them there. – Attersson May 14 '18 at 01:09
  • What does it exactly mean? – Bill May 14 '18 at 01:11
  • it means you can use all of the Object.prototype also with a Function. – Attersson May 14 '18 at 01:12
  • What if I don't really need to manipulate the data inside the Object? Is variables better in that case? I am just using it to store some data. – Bill May 14 '18 at 01:13
  • Like I said, they are inside your function, they are already tidy. Further wrapping them inside an Object provides no benefit. You can already perform all the operations you can do with Object, within the Function, because the Function is an extension of Object.. – Attersson May 14 '18 at 01:15
0

Option one would be more efficient as it provide more flexibility over variables, if you want to pass any value to any method you need not to process whole json. Make sure you name your variable properly.

Kushagra Misra
  • 461
  • 1
  • 7
  • 15
  • An object is not JSON and value["1"] is atomic access (single operation) not requiring to process the whole object. – Attersson May 14 '18 at 00:55