PHP Anonymous vs Arrow Function Performance Test
Benchmark. Performance differences between PHP anonymous functions and arrow functions in our detailed benchmark test. Optimize your PHP code efficiently!
edit_note
PHP Code Editor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
echo "<pre>";
function benchmark(callable $function, int $iterations): float {
$myArray = range(1,$iterations);
$start = microtime(true);
array_map($function, $myArray);
return microtime(true) - $start;
}
// Number of iterations for the benchmark
$iterations = 1000000;
// Benchmark for Anonymous Function
$anonymousFunction = function($value) {
return $value * 2;
};
benchmark(fn($value) => null, 1);
$timeArrow = benchmark(fn($value) => $value * 2, $iterations);
$timeAnonymous = benchmark($anonymousFunction, $iterations);
// Output results
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
settingsPHP Version
panorama_fish_eye
7.2
panorama_fish_eye
7.4
panorama_fish_eye
8.0
panorama_fish_eye
8.1
panorama_fish_eye
8.2
panorama_fish_eye
8.3
task_alt
8.4
panorama_fish_eye
8.5
terminal
Execution Result
play_circle_outline
Ready to execute
Click the "Run Script" button to see the output here
article
Description
PHP Anonymous vs Arrow Function Performance Test
Explore the performance differences between anonymous functions and arrow functions in PHP.
What are Anonymous and Arrow Functions?
-
Anonymous Functions: Also known as closures, these functions do not require a name and can be assigned to variables or passed as arguments.
-
Arrow Functions: Introduced in PHP 7.4, arrow functions provide a concise syntax for creating simple functions. They automatically capture variables from the surrounding scope.
Benchmarking Performance
We created a benchmark to compare the execution time of both function types. Below is the code used for testing
Comments
No comments yet
Be the first to share your thoughts!