Is everything in JavaScript an Object?

added by JavaScript Kicks
10/21/2014 11:01:16 AM


I’ve always found this statement confusing: “Everything in JavaScript is an Object”. What did they mean by this? How can a Function or an Array at the same time be an Object? Before we tackle this question, we need to understand how the different Data Types are categorized.


Drew Peterson
10/21/2014 2:23:55 PM
I didn't realize that wrapper objects were created to wrap primitives when calling functions on them. It's an interesting way of handling compare by value/reference on primitives without removing the ability to call methods on them. I'm a bit of a performance nut, so I wonder if there's any advantage, at a mass scale, to creating objects types rather than primitives when calling methods on them. So, for example, I have a large array of floating point numbers which I need to process. In doing so, I need to call toFixed(). I wonder if there's a point where storing as objects would be faster than as primitives since we'd avoid the wrapper object instantiation @andrewmrobbins?

Robert Greyling
10/21/2014 2:31:04 PM
It still feels to me like primitives would be the faster choice here, but it's only a feeling. Things that are closer to the metal are usually faster since parsers need to do less work regarding their operations. Maybe @andrewmrobbins has more insight here, but isn't comparrison by value here less memory intensive than the by ref comparrisons? I know people have fast PCs these days, but on mobile it could possibly make quite a difference?

Andrew Robbins
11/9/2014 12:07:20 AM
@drewpcodes & @RobertTheGrey First I want to apologize for not responding sooner =) Life as been insane to say the least! I read both of your comments the day you published them and found the idea very intriguing. I was curious to know the answer as-well so I wrote a small program to test the performance of each. You can find the program here: So what I did was create two arrays. One array stored the data as Objects, and another stored the data as Primitives. Based on your use-case Drew, the data I used was 150 floating point numbers. I also decided to limit both arrays to 150 entries so it wouldn't freeze my computer =) Within the program, I'm just telling it to iterate over the arrays and timestamp before and after every iteration. Then I do a little math on the time stamps and find the average in ms. For the sake of accuracy, I told the program to loop 5000 times for each instance. Surprisingly enough, the list containing the data stored as Objects was actually 2ms faster then stored as Primitives! Here were the actual numbers: // List with Objects Average execution across 5000 repetitions: 16.8528 ms // List with Primitives Average execution across 5000 repetitions: 18.2898 ms Would love any and all input on this. Take care guys, Andrew

Robert Greyling
11/10/2014 5:17:01 PM
Oh wow, I didn't see that coming - great POC. So I guess that means the wrapping/unwrapping isn't quite as expensive as initially thought. given how small the diff in perf, probably just makes sense to use the one that is easiest to program then :) Nice work @andrewmrobbins

Drew Peterson
11/10/2014 5:51:19 PM
Yes, thanks so much for testing this out @andrewmrobbins! Definitely above and beyond. @RobertTheGrey yes it would seem that wrapping the primitives in objects in order to call the method is more expensive than unwrapping the value later.