What causes a decrease in speed when a function is called without specifying its owner?

Let's consider the following scenario:

var abs = Math.abs;

Wouldn't abs(-10) be quicker than Math.abs(-10)? Since abs is called directly.

This caught my attention: Math.abs vs custom abs function

Update:

The same test conducted in Internet Explorer 11 yields a completely different outcome:

I believe this could be attributed to optimizations on Chrome's V8 Engine for built-in functions.

A test by nnnnnn that illustrates my point: Property shortcut

Answer №1

Givi made this answer irrelevant. Please see the comments for clarification.

It appears that searching for a user-defined function within a user-defined object is slower compared to searching for a function bound to a local variable, which aligns with your initial assumption.

Interestingly, accessing Math.* functions seems to be quicker, possibly due to internal optimizations within the V8 engine. This means that storing built-in functions in a local variable (i.e., "caching") may actually result in slower performance, while caching user-defined functions can lead to faster execution.

For further insights and proof of concept, you can refer to this JavaScript performance test, showcasing the speed discrepancies between Math.* functions and their equivalent var x = Math.x versions.

In addition, I came across a line in your question suggesting optimization of built-in functions in Chrome's V8 Engine:

I'd speculate that this is due to some optimizations on built-in functions in Chrome's V8 Engine.

While I cannot confirm it with absolute certainty, there does seem to be evidence supporting this theory.

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

Unable to showcase information in a jQuery UI popup through AJAX when initially presented

I'm trying to display reviews from my database on a jQuery UI dialog box upon loading, but nothing is showing up. Here are the reviews: {"results":[{"review_text":"good"},{"review_text":"not bad"},{"review_text":"great"}]} Can someone please check m ...

Create interactive highcharts graphs using data from a CSV file generated through PHP

I'm having trouble working with Highcharts and csv data. Take a look at this example: http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/line-ajax/ $.getJSON('http://www.highcharts.com/ ...

Displaying a page using express.Router()

I'm having trouble figuring out how to incorporate EJS rendering into a file utilizing express.Router(). The usual method of router.set('view engine', 'ejs') doesn't seem applicable in this context. Any ideas on how I can succ ...

When dealing with errors in fetching JSON data, consider implementing a robust error handling strategy and

Is there a way to provide a fallback JSON string in case the fetch URL fails to connect? const Response3 = await fetch(`https://example.com/match/get?_=&id=${matchlist[2].id}`) ...

Automatically adjust Kendo Grid column widths, with the option to exclude a specific column from

Seeking a solution to dynamically adjust column widths in a Kendo Grid using column(index = n) or column(headingText = 'Address'). For automatically fitting column widths, the following approach can be used: <div id="example ...

The image selection triggers the appearance of an icon

In my current project, I am working on implementing an icon that appears when selecting an image. The icon is currently positioned next to the beige image, but I am facing difficulties in making it disappear when no image is selected. Below are some image ...

The drag and drop feature seems to be malfunctioning in the Selenium Webdriver when using the Node.js (JavaScript) test automation framework

I am currently utilizing the selenium webdriver along with a customized automation framework built in nodejs. My goal is to implement drag and drop functionality for a slider using the actions class, but unfortunately I am encountering issues. Below you ca ...

Retrieving a specific key-value pair from an object within a JavaScript array

Looking to extract a specific value from an array of objects using array.map? Check out the code snippet below: let balanceInfo = students.map((student) => { if (typeof(student) === Object){ let balance = student.balance; return balanc ...

Pausing and then resuming an interval function within the same function

I'm struggling with an $interval function that runs every second. The function retrieves user credentials from local storage and checks if they have expired. If they have, it refreshes them with new ones. Otherwise, it does nothing. This is what my ...

What steps does VSCode take to ensure that all necessary node dependencies are installed for its extensions?

As I work on constructing my own pluggable application, I've been curious about how vscode handles its extensions' dependencies. Does it run npm install in each extension directory and utilize those installations individually? Or does it have a c ...

Speeding up the loading time of my background images

body { background: url(http://leona-anderson.com/wp-content/uploads/2014/10/finalbackgroundMain.png) fixed; background-size:100% auto; } I have unique background images on each of my sites, but they are large in size and take some time to load due to bein ...

bxSlider adds in an empty slide after deleting an image

Whenever I click a link in my navigation, I want to remove certain images from the page. I tried implementing a solution, but now I have two empty spaces where the images used to be. How can I remove those as well? I searched on Stack Overflow for a soluti ...

Interact with Dialogflow using fulfillment to retrieve responses by opening an HTTP URL

Despite my efforts, I have yet to find a satisfactory solution. I am in the process of creating an agent on Dialogflow. This agent will be embedded on a webpage, not as part of Facebook Messenger or Google Assistant. The purpose of this agent is to guide ...

Unique: "Best Practices for Setting Angular.js Controller Data Directly in the Code"

In this scenario, I need to initialize the data from an inline script, even though I know how to achieve this using a promise on an http request. Currently, the controller is already defined in the header js: var testModule = angular.module('myTestM ...

Tips for obtaining a specific sorting order based on a wildcard property name

Here's the structure of my JSON object, and I need to sort it based on properties starting with sort_ { "sort_11832": "1", "productsId": [ "11832", "160", "180" ], "sort_160": "0", "sort_180": " ...

Received a syntax error from the DOM parser even after successfully downloading it through NPM

Seeking guidance as a beginner in this technology field. I have installed dom-parser from the NPM package. However, when I tried to declare it below, I encountered a syntax error. Although it seems like a minor issue, I am puzzled and unsure of where I am ...

What is the purpose of requiring the explicit invocation of app.listen(port) to enable express-ws to function properly?

I've recently started exploring NodeJS Express and came across the official tutorial from express-ws for setting up websockets in a simple project generated using npx express-generator. While following the tutorial, I noticed that in the app.js file, ...

Using v-on:click to dynamically update multiple sections of content in a Vue.js and Liquid environment

Whenever I click on a button, I want the text and image to change. I attempted to do this but encountered difficulties in updating multiple elements simultaneously. {% for variant in product.variants %} <label for="variant_{{- variant.id }}"&g ...

What is the best way to integrate external JavaScript libraries into Meteor.js?

When working with Meteor, how can I incorporate a 3rd party JavaScript library like gridster.js (http://gridster.net/)? Typically, I would include the script directly in the HTML page. However, is there a way to require it directly in the JavaScript file, ...

Is there a way to confirm if the target has been successfully removed from the element using jQuery?

$(".dropdown-toggle").click(function(event) { var target = $(event.target); if (target.is(this)) { $(this).find(".caret").toggleClass("customcaret"); } }); <div class="dropdown-toggle"> <div class="caret"></div> </div> ...