TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVASCRIPT

JavaScript arguments (Function)

Use the arguments object in a function. Test the performance of arguments usage.
Arguments. This is a special object that is received in a function. Browsers contain many optimizations for arguments objects. We can access arguments like an array.
For performance, using an "arguments" object is worse than having named parameters. But sometimes a method must receive a variable number of arguments.
Arguments example. Here is a simple program that uses the special arguments object. We can access the length, and then access each value at an index.

Info: With arguments, we can make "varargs" or "params" methods in JavaScript. Any number of arguments can be passed.

JavaScript program that uses arguments function printAnimals() { "use strict"; // Access arguments (a special variable available). for (var i = 0; i < arguments.length; i++) { // Get each argument at an index. console.log("ANIMAL NAME: " + arguments[i]); } } // Call the function. printAnimals("bird", "frog", "dog"); printAnimals("leopard"); Output ANIMAL NAME: bird ANIMAL NAME: frog ANIMAL NAME: dog ANIMAL NAME: leopard
Benchmark, arguments. Is accessing arguments fast? Each version does the same thing—we pass 3 arguments to a method and it returns the product of the numbers.

Version 1: We access in the "func1" function the special arguments object. We use var to assign to the arguments.

Version 2: We create a temporary array and pass it to the "func2" function. An allocation must occur on each function call.

Result: In 2019, using an array seems to be faster in Chromium than using arguments.

JavaScript program that benchmarks arguments object function func1() { // Multiply 3 arguments together. var s1 = arguments[0], s2 = arguments[1], s3 = arguments[2]; return s1 * s2 * s3; } function func2(array) { // Multiply 3 array elements together. var s1 = array[0], s2 = array[1], s3 = array[2]; return s1 * s2 * s3; } var x1 = performance.now(); // Version 1: uses arguments object. for (var i = 0; i < 10000000; i++) { func1(10, 20, 2); } var x2 = performance.now(); // Version 2: use array argument. for (var i = 0; i < 10000000; i++) { func2([10, 20, 2]); } var x3 = performance.now(); // Results. console.log("TIME 1: " + (x2 - x1)); console.log("TIME 2: " + (x3 - x2)); Output TIME 1 (2019): 24.489 TIME 2: 22.255
A warning. Usually if we have a function that must receive a known number of arguments, using named parameters is much faster. So be careful with arguments.Arguments object: mozilla.org
Another warning. Arguments is hard to use optimally. In my testing, using both named parameters and arguments tends to be bad for performance.

Also: The length property on arguments can be accessed. And arguments can be passed to other methods without much trouble.

Tip: Always test in a benchmark harness (like the ones on this page) before changing or adding arguments usage.

A summary. Arguments is a powerful feature in JavaScript. But usually arrays, and named arguments, are a simpler and clearer choice for function design.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf