Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way of handling high volumes of promises?

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?

like image 698
jordan Avatar asked Nov 23 '19 20:11

jordan


People also ask

Why is it important to keep your promises to others?

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.

Why is it important to keep promises made to customers?

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.

What is promise based management?

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.

What are the two ways to handle 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.

What is high call volume and how to reduce it?

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?

How can I optimize transactions in high volume OLTP databases?

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.

What are the arguments of a promise?

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.


1 Answers

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
    })
  }
})
like image 60
Jeevan HS Avatar answered Nov 15 '22 00:11

Jeevan HS