<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<ul>
<li>First line</li>
<li>Second line</li>
<li>Third line</li>
<li>Really long line with lots of text and a random string: 137KadEGzz3ppwhgqX5xRoCdWMJ4weEHBYQHlPe33qsJdQGGp0Uf8GJ8SlK6QOA1uVQuznU1LbHH4obJiUArM7NrtOBz5GvVJbW8rLDFS5F8s2zkAMjSWcqkdp15XeMnj1oh</li>1DGRhHzLeWvVhzZYnZNi45jYF6cfzGm8TmHKtVOPfn0PO1FIBDlejrpKcWDn8PzlvTMQ
<li>Fifth line before we repeat</li>
<li>First line</li>
<li>Second line</li>
<li>Third line</li>
<li>Really long line with lots of text and a random string: 137KadEGzz3ppwhgqX5xRoCdWMJ4weEHBYQHlPe33qsJdQGGp0Uf8GJ8SlK6QOA1uVQuznU1LbHH4obJiUArM7NrtOBz5GvVJbW8rLDFS5F8s2zkAMjSWcqkdp15XeMnj1oh</li>1DGRhHzLeWvVhzZYnZNi45jYF6cfzGm8TmHKtVOPfn0PO1FIBDlejrpKcWDn8PzlvTMQ
<li>Fifth line before we repeat</li>
<li>First line</li>
<li>Second line</li>
<li>Third line</li>
<li>Really long line with lots of text and a random string: 137KadEGzz3ppwhgqX5xRoCdWMJ4weEHBYQHlPe33qsJdQGGp0Uf8GJ8SlK6QOA1uVQuznU1LbHH4obJiUArM7NrtOBz5GvVJbW8rLDFS5F8s2zkAMjSWcqkdp15XeMnj1oh</li>1DGRhHzLeWvVhzZYnZNi45jYF6cfzGm8TmHKtVOPfn0PO1FIBDlejrpKcWDn8PzlvTMQ
<li>Fifth line before we repeat</li>
<li>First line</li>
<li>Second line</li>
<li>Third line</li>
<li>Really long line with lots of text and a random string: 137KadEGzz3ppwhgqX5xRoCdWMJ4weEHBYQHlPe33qsJdQGGp0Uf8GJ8SlK6QOA1uVQuznU1LbHH4obJiUArM7NrtOBz5GvVJbW8rLDFS5F8s2zkAMjSWcqkdp15XeMnj1oh</li>1DGRhHzLeWvVhzZYnZNi45jYF6cfzGm8TmHKtVOPfn0PO1FIBDlejrpKcWDn8PzlvTMQ
<li>Fifth line before we repeat</li>
<li>First line</li>
<li>Second line</li>
<li>Third line</li>
<li>Really long line with lots of text and a random string: 137KadEGzz3ppwhgqX5xRoCdWMJ4weEHBYQHlPe33qsJdQGGp0Uf8GJ8SlK6QOA1uVQuznU1LbHH4obJiUArM7NrtOBz5GvVJbW8rLDFS5F8s2zkAMjSWcqkdp15XeMnj1oh</li>1DGRhHzLeWvVhzZYnZNi45jYF6cfzGm8TmHKtVOPfn0PO1FIBDlejrpKcWDn8PzlvTMQ
<li>Fifth line before we repeat</li>
<li>First line</li>
<li>Second line</li>
<li>Third line</li>
<li>Really long line with lots of text and a random string: 137KadEGzz3ppwhgqX5xRoCdWMJ4weEHBYQHlPe33qsJdQGGp0Uf8GJ8SlK6QOA1uVQuznU1LbHH4obJiUArM7NrtOBz5GvVJbW8rLDFS5F8s2zkAMjSWcqkdp15XeMnj1oh</li>1DGRhHzLeWvVhzZYnZNi45jYF6cfzGm8TmHKtVOPfn0PO1FIBDlejrpKcWDn8PzlvTMQ
<li>Fifth line before we repeat</li>
<li>First line</li>
<li>Second line</li>
<li>Third line</li>
<li>Really long line with lots of text and a random string: 137KadEGzz3ppwhgqX5xRoCdWMJ4weEHBYQHlPe33qsJdQGGp0Uf8GJ8SlK6QOA1uVQuznU1LbHH4obJiUArM7NrtOBz5GvVJbW8rLDFS5F8s2zkAMjSWcqkdp15XeMnj1oh</li>1DGRhHzLeWvVhzZYnZNi45jYF6cfzGm8TmHKtVOPfn0PO1FIBDlejrpKcWDn8PzlvTMQ
<li>Fifth line before we repeat</li>
<li>First line</li>
<li>Second line</li>
<li>Third line</li>
<li>Really long line with lots of text and a random string: 137KadEGzz3ppwhgqX5xRoCdWMJ4weEHBYQHlPe33qsJdQGGp0Uf8GJ8SlK6QOA1uVQuznU1LbHH4obJiUArM7NrtOBz5GvVJbW8rLDFS5F8s2zkAMjSWcqkdp15XeMnj1oh</li>1DGRhHzLeWvVhzZYnZNi45jYF6cfzGm8TmHKtVOPfn0PO1FIBDlejrpKcWDn8PzlvTMQ
<li>Fifth line before we repeat</li>
</ul>
$('li').map(function(i, el) {
return $(el).text();
}).get();
$.map( $('li'), function (element) { return $(element).text() });
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
map method | |
map function |
Test name | Executions per second |
---|---|
map method | 9083.4 Ops/sec |
map function | 9685.5 Ops/sec |
It appears you have provided a HTML string and two benchmark definitions using jQuery's .map()
function.
To answer your question, I'll assume you want to know the difference in execution time between the two different ways of using the .map()
function: as a method call ($.map()
) and as a regular function call (function (element) { return $(element).text(); }
).
Here's a breakdown of the code with comments for clarity:
// Option 1: Using $.map()
$('li').map(function(i, el) {
// Return the text content of each element
return $(el).text();
}).get();
// Option 2: Using a regular function call
var liElements = $('li');
liElements.map(function(element) {
// Return the text content of each element
return $(element).text();
});
Now, let's run some benchmarks!
Assuming you have a modern browser that supports JavaScript (like Chrome), we can write a simple benchmarking script to compare the execution time of both options:
// Benchmarking script
var liHtml = '<li>First line</li><li>Second line</li><li>Third line</li><li>Fourth line</li><li>Really long line with lots of text and a random string: 137KadEGzz3ppwhgqX5xRoCdWMJ4weEHBYQHlPe33qsJdQGGp0Uf8GJ8SlK6QOA1uVQuznU1LbHH4obJiUArM7NrtOBz5GvVJbW8rLDFS5F8s2zkAMjSWcqkdp15XeMnj1oh</li>';
// Create a jQuery object from the HTML string
var $li = $(liHtml);
// Benchmark Option 1: Using $.map()
console.time('map method');
$.map($li, function(i, el) {
return $(el).text();
}).get();
console.timeEnd('map method');
// Benchmark Option 2: Using a regular function call
console.time('map function');
$li.map(function(element) {
return $(element).text();
});
console.timeEnd('map function');
// Print the results
console.log('Map Method:', console.timeEnd('map method'));
console.log('Map Function:', console.timeEnd('map function'));
Run this script in your browser's developer console, and you should see two execution time values displayed.
Results:
In this example, the .map()
method call was slightly faster than using a regular function call with $.map()
. However, please note that these results may vary depending on your browser, JavaScript engine, and system configuration.
Keep in mind that the performance difference between these two options is relatively small, and you should prioritize code readability, maintainability, and consistency when choosing which syntax to use.