<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
const p = new Promise((resolve) => {
setTimeout(() => {
resolve(1)
}, 500)
})
p.then((result) => {
console.log(result)
})
const dfd = new $.Deferred();
setTimeout(() => {
dfd.resolve(1)
}, 500)
$.when(dfd).then((result) => {
console.log(result)
})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
promise | |
jquery |
Test name | Executions per second |
---|---|
promise | 480630.2 Ops/sec |
jquery | 20631.2 Ops/sec |
I'd be happy to help you understand the JavaScript microbenchmark at MeasureThat.net.
Overview
The benchmark compares two approaches: using Promises and using jQuery's Deferred object (specifically, $.Deferred) to resolve asynchronous code. The goal is to measure which approach performs better in terms of execution speed.
Options Compared
There are only two options being compared:
Pros and Cons
Here are some pros and cons of each approach:
Promise
Pros:
Cons:
jQuery Deferred
Pros:
Cons:
Library: jQuery Deferred
In this benchmark, $.Deferred
is used as part of the jQuery library. A deferred object is a way to manage asynchronous operations and handle their resolution or rejection in a more elegant and flexible way. It's essentially a container for promises that can be resolved or rejected after some time has passed.
Special JS Feature/ Syntax
There are no special JavaScript features or syntax being tested in this benchmark, apart from the use of Promises and jQuery Deferred objects, which are both part of the standard language.
Other Alternatives
If you're looking for alternatives to promises or jQuery Deferred, here are a few options:
co
or bluebird
, which provide more advanced cooperative scheduling capabilities.Keep in mind that these alternatives may have different trade-offs in terms of complexity, performance, and compatibility.