Varied browser speeds noticed when generating empty arrays of set sizes

Observing unusual performance data when creating empty arrays using Array(length) in Chrome (85), Firefox(79), and Safari(11.1) on a Mac OS X (10.13.4) system.

Below is the test script used;

console.time('Array(length)');
var len_int = 1;
var test_arr = Array(len_int);
console.timeEnd('Array(length)');
console.log('test_arr.length =', test_arr.length);

Testing various values of len_int from 1 to 1000000000 [one billion]. Here are the results:

CHROME

Performance stats:
Array(length) - 1: 0.018310546875 ms
Array(length) - 10: 0.014892578125 ms
Array(length) - 100: 0.01171875 ms
Array(length) - 1000: 0.0068359375 ms
Array(length) - 10000: 0.011962890625 ms
Array(length) - 100000: 0.187744140625 ms
Array(length) - 1000000: 1.826904296875 ms
Array(length) - 10000000: 18.211181640625 ms
Array(length) - 100000000: 0.026123046875 ms
Array(length) - 1000000000: 0.0126953125 ms

FIREFOX

Performance stats:
Array(length) - 1: 1ms - timer ended
Array(length) - 10: 1ms - timer ended
Array(length) - 100: 0ms - timer ended
Array(length) - 1000: 0ms - timer ended
Array(length) - 10000: 2ms - timer ended
Array(length) - 100000: 1ms - timer ended
Array(length) - 1000000: 0ms - timer ended
Array(length) - 10000000: 0ms - timer ended
Array(length) - 100000000: 1ms - timer ended
Array(length) - 1000000000: 1ms - timer ended

SAFARI

Performance stats:
Array(length) - 1: 0.006ms
Array(length) - 10: 0.028ms
Array(length) - 100: 0.011ms
Array(length) - 1000: 0.009ms
Array(length) - 10000: 0.031ms
Array(length) - 100000: 0.252ms
Array(length) - 1000000: 1.663ms
Array(length) - 10000000: 19.373ms
Array(length) - 100000000: 696.767ms
Array(length) - 1000000000: 0.008ms

The abnormality lies in Chrome's sudden drop in performance for array sizes between 1000000 [1.8ms] and 10000000 [18ms], before returning below sub-millisecond levels for larger arrays. Safari shows a decline in performance from 1000000 [19ms] to 100000000 [696ms], but then improves again at 1000000000 [0.008ms]. Firefox, on the other hand, remains relatively consistent in performance.

Why does this anomaly occur? Is there a solution or alternate method that maintains high performance?

It's worth noting that this benchmark was run multiple times, each showing the same outcome. The same discrepancy was observed on an iPhone 7 running Safari (via BrowserStack).

This inquiry arises as I use Array(length) to optimize scripts relying heavily on arrays, but notice a loss in performance benefits within the array size range of 1000000 to 10000000 on Chrome and Safari.

Answer №1

Relying on a single run of microbenchmarks can be unreliable due to the potential errors introduced by other running processes and compiler optimizations.

For more accurate results, it is recommended to allow the code to execute multiple times and ensure that the array is actively used to prevent any premature optimization by the compiler.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide on incorporating multiple nested loops within a single table using Vue.js

Is it possible to loop through a multi-nested object collection while still displaying it in the same table? <table v-for="d in transaction.documents"> <tbody> <tr> <th>Document ID:</th> &l ...

The offspring of a React component

How can I select a specific div in children using React without passing it as a prop? I want to transform the code snippet from: <Pane label="Tab 1"> <div>This is my tab 1 contents!</div> </Pane> to: <Pane> <div&g ...

Using JavaScript, append a hidden input field in Yii2 and retrieve it from the controller

Seeking assistance as a newcomer to yii2. I'm looking for help in resolving an issue that has arisen. I'm attempting to add a hidden input field to my form using JavaScript (not linked to a model). Subsequently, when I submit the form, I want to ...

"Customize the number of items displayed per page with Bootstrap Vue's perPage

I am currently working on a Vue project which you can view on codesandbox and utilizing bootstrap-vue. Within the project, there are multiple columns containing cards along with pagination: <template> <b-container> <b-row :cu ...

Tips for managing an array: A useful tool for storing multiple elements

public class Student implements Comparable<Student> { String fName; String sName; String subject[]; int subjectValuation; public Student(String fName, String sName, String[] subject, int subjectValuation) { ...

The configuration object provided is invalid. Webpack initialization is using a configuration object that does not adhere to the API schema for Webpack 5

I'm facing an issue while configuring Webpack 5. The error message that appears during the npm run serve command in the CLI is as follows: ╰─$ npm run serve > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6d39bc6 ...

Obtain data from a popup window and transfer it back to the main parent window

I am having trouble with a pop-up window that contains selections. After the user chooses options, I want those selected options to appear in the main window without refreshing it. I am utilizing JavaScript for this task, but I am struggling to find a way ...

HTML - Retain placeholder text while user inputs

My input is structured like this: <input value="My text" placeholder="Placeholder"> Typing in the input causes the placeholder text to disappear, which is expected. However, I am looking to keep the placeholder text visible as a background behind ...

Error event triggered by Ajax call despite receiving 200 ok response

$.ajax({ url: 'http://intern-dev01:50231/api/language', type: 'GET', dataType: 'json', success: function() { console.log('Success! The call is functioning.'); }, ...

Using JavaScript along with Ajax and JSON allows for sending complex data structures such as objects and

One of the features on my form allows users to input information about their clients. This versatile form enables users to add multiple phone numbers, email addresses, and physical addresses without any limitations. When adding a phone number or an email ...

Ways To Create Multiple HTML Players

After successfully creating an audio player with html, css and javascript, I now face the challenge of adding multiple players to a single page. However, when attempting to play any player besides the first one, it only plays the initial player. How can ...

Steps to display a NotFound page when the entered path does not match the route in module federation React micro frontends

Typically, if the provided path does not match, we display the NotFound page to the user using the following code snippet <Route path={"*"} component={NotFound} /> However, upon adding this code, I always get redirected to the home page ( ...

Arrange a bootstrap table according to the specified column index

Typically, bootstrap table sorting is based on the first column. If you want to change this default behavior, you can use data-sort-name="column_name" data-sort-order="asc/desc", as explained in this link. In my case, the columns are d ...

Is my Socket.io application consuming excessive bandwidth? What might be causing this issue?

Upon reviewing my AWS billing report, I noticed an excessive data transfer of 495.385 GB on my socket.io application running on the EC2 instance. This amount seems too high for a small experimental website like mine. Could this be due to inefficient code i ...

extract data from text using regular expressions to retrieve two values

Can someone assist with creating a regex expression to parse this string in JavaScript: $D('make').onChange('s',123456789,'a',10) I am trying to extract the values 123456789 and a from this string. Any help would be appreci ...

Extracting keys and values from a JSON string for analysis

My current code for the service now rest outbound call is functioning correctly. However, I am facing issues while trying to parse the JSON body in the second REST call and fetch values in the desired table format. try { var r = new sn_ws.RESTMessageV2 ...

Preventing jQuery plugin from overriding default settings

I have created a jQuery plugin using the nested namespace pattern, inspired by the design template found in Addy Osmani's book. The plugin is a simple gallery and everything seems to be functioning correctly. However, I am facing an issue when attemp ...

What causes CSS to fail to load in React but work normally in Next.js?

We are currently experiencing an issue with a component located in a Git Submodule that is being used by both Next.js and React. While everything is functioning correctly in Next.js, React is unable to accept the way the CSS is being loaded: import styles ...

Empty req.body in Express JS is not fulfilling the necessary data requirements

I've been racking my brain for hours trying to figure out why req.body is showing up empty. I've scoured every corner of stackoverflow and tried every solution that I could find, but nothing seems to be working. Express.js POST req.body empty ...

The returned data from a Apollo Client useMutation call is consistently undefined

Currently, I have a code snippet that executes a mutation to update a post to "published." The mutation functions correctly and updates the data as intended. However, I am facing an issue where the data property remains undefined in the useMutation hook. S ...