What is the most efficient way of handling high volumes of promises? I've come up with 2 solutions and determined that Solution 2 (which uses bluebird's promise.map
) is faster.
Solution 1 (~38ms per file)
readFile(file) {
return new Promise((resolve, reject) => {
jsmediatags.read(file, {
onSuccess: resolve,
onError: reject
})
})
}
async readFilesHandler() {
console.time('readFilesHandler timer')
const fileArray = Array.from(this._fileSelectInput.files)
const tracksArray = []
for (let file = 0; file < fileArray.length; file++) {
await this._readFile(fileArray[file]).then(tags => {
tracksArray.push({
id: file + 1,
title: tags.tags.title || undefined,
artist: tags.tags.artist || undefined,
album: tags.tags.album || undefined,
year: tags.tags.year || undefined
})
})
}
this.dispatchEvent(new CustomEvent('tracks-selected', {
detail: tracksArray
}))
console.time('readFilesHandler timer') // ~38ms/file
}
Solution 2 (~32ms per file)
_readFiles() {
console.time('_readFiles timer')
const fileArray = Array.from(this._fileSelectInput.files)
window.Promise.map(fileArray, file => {
return new Promise((resolve, reject) => {
jsmediatags.read(file, {
onSuccess: resolve,
onError: reject
})
})
}, {
concurrency: 5
}).then(tags => {
const results = tags.map((tag, index) => ({
id: index + 1,
title: tag.tags.title || undefined,
artist: tag.tags.artist || undefined,
album: tag.tags.album || undefined,
year: tag.tags.year || undefined
}))
this.dispatchEvent(new CustomEvent('tracks-selected', {
detail: results
}))
})
console.timeEnd('_readFiles timer') // ~32ms/file
}
Are there ways of achieving the same result that are even more performant?
Promises are commitmentsPeople with strong relationships rank higher in emotional intelligence and are more likely to stay loyal to their commitments. Whether the commitment is to yourself or to someone else, making a promise is a commitment that you will keep your word. It is a commitment that reinforces trust.
Companies that succeed in keeping promises to customers often, gain a reputation of being reliable and trustworthy. This in turn enhances the reputation and reach of a company since existing customers tend to share their great experiences with others.
Promise-based management empowers individuals to act like true entrepreneurs within the organization—to spot opportunities, assemble the resources required to seize those opportunities, and adjust on the fly. Within the bounds of the firm's objectives, employees can own and run their own personal networks of promises.
There are two ways to handle promises: async/await or the .then method. What is a promise? A promise is NOT A FUNCTION. A promise is an OBJECT.
Call volume refers to the number of calls a business or call center receives. High call volume occurs when a business gets more inbound calls than its employees can reasonably accommodate. Many companies define high call volume as a call volume that is 10% above the expected level for their team. What Causes High Call Volume?
One of the most effective techniques I have found to optimize transactions in high volume OLTP databases is to persist your data in sets. Once you have a set of data available in SQL Server, you need to process that set using efficient SQL Syntax.
After the promise is created, the promise will automatically call the executor. This executor has two arguments dubbed “resolve” and “reject” which are callback functions. The promise has two properties called “state” and “result” which are initially set to “pending” and “undefined,” respectively.
You could use Promise.allSettled()
Entire Promise array is resolved as fast as the slowest individual promise is resolved.
const bucket = await Promise.allSettled(fileArray.map(file => this._readFile(file)))
bucket.forEach(file => {
if (file.value) {
const tags = file.value;
tracksArray.push({
id: file + 1,
title: tags.tags.title || undefined,
artist: tags.tags.artist || undefined,
album: tags.tags.album || undefined,
year: tags.tags.year || undefined
})
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With