<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div id="nhngodo">Hi! NHNgodo!</div>
<script>
window._getbyid = {};
function getbyid(elId){
if(!window._getbyid[elId]){
window._getbyid[elId] = document.getElementById(elId);
}
return window._getbyid[elId];
}
</script>
var el = document.getElementById("nhngodo");
el.innerText = "Hi! NHNgodo!";
$("#nhngodo").text("Hi! NHNgodo!");
getbyid("nhngodo").innerText = "Hi! NHNgodo!";
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Vanilla JS | |
Jquery | |
getbyid |
Test name | Executions per second |
---|---|
Vanilla JS | 1102079.8 Ops/sec |
Jquery | 224086.7 Ops/sec |
getbyid | 1253308.5 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark compares the performance of three JavaScript approaches:
getbyid
libraryScript Preparation Code
The script preparation code is empty ("Script Preparation Code": null
), which means that no special setup or initialization is required for the benchmark.
Individual Test Cases
There are three test cases:
var el = document.getElementById("nhngodo"); el.innerText = "Hi! NHNgodo!");
document.getElementById
and string manipulation.$("#nhngodo").text("Hi! NHNgodo!");
$
and .text()
).getbyid("nhngodo").innerText = "Hi! NHNgodo!");
getbyid
function that wraps native JavaScript's document.getElementById
.Library Explanation
The custom getbyid
library is not a standard JavaScript library. It appears to be a simple wrapper around native JavaScript's document.getElementById
, caching the result for future lookups.
Special JS Features or Syntax
There are no special JavaScript features or syntax used in this benchmark. The code uses standard JavaScript syntax and does not rely on any experimental features.
Pros and Cons of Different Approaches
Here's a brief summary of the pros and cons of each approach:
Other Alternatives
Some alternative approaches that are not included in this benchmark include:
These alternatives may offer better performance or features than the custom getbyid
library, but they also come with additional overhead and complexity due to the framework or library itself.