Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
Parsing big JSON file using Typescript

I want to parse/process a 25 MB JSON file using Typescript and filter out/sort the objects .. The code I wrote is taking minutes (and sometimes timeouts) not sure why is this happening or if there's another way to make the code more efficient.

Note: the code worked on a small file

import fs from 'fs';
searchAccounts(): Promise<Account[]> {
       const accountSearchCriteria: AccountSearchCriteria = {
                country: 'NZ',
                mfa: 'SMS',
                name: 'TEST',
                sortField: 'dob'
        };
        const jsonPath = './src/file.json';
        const rawAccounts = fs.readFileSync(jsonPath, 'utf-8');
        let accounts: Account[] = JSON.parse(rawAccounts);
        if (accountSearchCriteria) {
            if (accountSearchCriteria.name) {
                accounts = accounts.filter(
                    account =>
                        account.firstName.toLowerCase() ===
                            accountSearchCriteria.name.toLowerCase() ||
                        account.lastName.toLowerCase() ===
                            accountSearchCriteria.name.toLowerCase()
                );
            }
            if (accountSearchCriteria.country) {
                accounts = accounts.filter(
                    account =>
                        account.country.toLowerCase() ===
                        accountSearchCriteria.country.toLowerCase()
                );
            }
            if (accountSearchCriteria.mfa) {
                accounts = accounts.filter(
                    account => account.mfa === accountSearchCriteria.mfa
                );
            }
            if (accountSearchCriteria.sortField) {
                accounts.sort((a, b) => {
                    return a[accountSearchCriteria.sortField] <
                        b[accountSearchCriteria.sortField]
                        ? -1
                        : 1;
                });
            }
            return accounts;
        }
        return accounts;
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Since your data size is 25 MB, you should use a more memory-efficient sorting algorithm.

You can try to use cycle sort.

cycle-sort you can find an implementation here and use it in your code to see if there is a difference.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Node.js is single-threaded if your code blocking the thread for a long time it will give you a timeout error. There are two problems with your code.

  1. you are using fs.readFileSync(jsonPath, 'utf-8');, it is an asynchronous function and blocks the thread while reading the file. Use instead fs.readFile('./Index.html', callback):
const fs = require('fs');
fs.readFile('./Index.html', function read(err, data) {
   if (err) {
       throw err;
   }
   console.log(data);
});
  1. Sorting data is also a thread-blocking task try a different sorting technique, which doesn't occupy thread for a long time.

Note: Node.js is not good with CPU-centric tasks i.e sorting, image processing, etc. It's good with I/O tasks.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda