Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Undefined vs null
(version: 0)
Comparing performance of:
setting null vs setting undefined
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function ctor() { var obj = { a: { a: 5 }, b: { b: 5 }, c: { c: 6 }, d: 5, e: [Math.random(), Math.random()] }; return obj; }
Tests:
setting null
var objects = []; for (var i = 0; i < 1000; i++) { objects[i] = ctor(); } var obj = objects[Math.floor(Math.random() * 1000)]; window.leak = obj.a + obj.b + obj.c + obj.d + obj.e; for (var i = 0; i < 1000; i++) { obj.a = null; obj.b = null; obj.c = null; obj.d = null; obj.e = null; }
setting undefined
var objects = []; for (var i = 0; i < 1000; i++) { objects[i] = ctor(); } var obj = objects[Math.floor(Math.random() * 1000)]; window.leak = obj.a + obj.b + obj.c + obj.d + obj.e; for (var i = 0; i < 1000; i++) { obj.a = void 0; obj.b = void 0; obj.c = void 0; obj.d = void 0; obj.e = void 0; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
setting null
setting undefined
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
setting null
43529.4 Ops/sec
setting undefined
44489.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a benchmark test for measuring the performance difference between using `null` and `undefined` as values in JavaScript objects. **Benchmark Definition** The benchmark definition is created using two different scripts: 1. The first script creates an object with properties that contain both numbers and arrays, and then assigns each property to be either `null`, `undefined`, or a random number. This creates a situation where the garbage collector may need to kick in to collect the objects. 2. The second script is identical to the first one, but instead of assigning values to the object properties, it sets them to `undefined` using the `void 0` syntax. **Options being compared** The two options being compared are: * Using `null` as a value * Using `undefined` as a value These two options are compared in terms of how quickly each one triggers the garbage collector and frees up memory, which is typically measured by the browser's execution time for the benchmark test. **Pros and Cons** * Using `null`: This can be faster because JavaScript engines tend to handle `null` values more efficiently than `undefined` values. * Using `undefined`: However, this approach may not lead to the same level of memory deallocation as using `null`, which could potentially improve performance in certain scenarios. **Library and its purpose** In both benchmark scripts, a library called ` ctor()` is used. This function is responsible for creating an object with properties that will be used for testing purposes. The purpose of this library is to provide a reusable and reproducible test case that can be executed multiple times to measure performance differences between using `null` and `undefined`. **Special JS feature or syntax** The benchmark script uses the `void 0` syntax, which is equivalent to `null`. This syntax is used instead of simply writing `null` as the value to be assigned to an object property. The use of this syntax may be intended to provide additional clarity or readability to the test case. **Other alternatives** There are several alternative approaches that could have been taken for this benchmark test, including: * Using a garbage collection framework such as V8's JavaScript engine, which has built-in mechanisms for measuring and optimizing garbage collection performance. * Implementing a custom garbage collector using a language like C++ to measure the performance impact of different values on memory deallocation. * Using a profiling tool or library to measure the execution time and memory usage of the benchmark test in different browsers or environments. Overall, the benchmark test is well-structured and provides a clear comparison between two options: using `null` versus `undefined`. However, the use of the `ctor()` library and the `void 0` syntax may be specific to the testing framework or environment being used.
Related benchmarks:
Return true vs return;
Variable assignment from object | traditional vs destructuring
Object speard vs assign
typeof first or second
in vs not undefined
Comments
Confirm delete:
Do you really want to delete benchmark?