Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Topaz tradutor
(version: 0)
Comparing performance of:
forEach no dicionário vs forEach no JSON
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
forEach no dicionário
dictionary = () => ({ tipoTransacao: 'type', movimentacaoExtrato: 'title', valor: 'value', dataHora: 'dateTime', saldoFinal: 'currentBalance', saldoDisponivel: 'balance', limiteChequeEspecial: 'overdraftLimit', saldoDisponivel: 'available', limiteTotalDisponivel: 'totalOverdraftLimit', totalInvestido: 'totalInvested', irpf: 'irpf', iof: 'iof', totalLiquido: 'netTotal', codigoProfissao: 'professionCOde', descricaoProfissao: 'professionDescription', tipoTransacao: 'type', movimentacaoExtrato: 'title', valor: 'value', dataHora: 'dateTime', saldoFinal: 'currentBalance', saldoDisponivel: 'balance', limiteChequeEspecial: 'overdraftLimit', saldoDisponivel: 'available', limiteTotalDisponivel: 'totalOverdraftLimit', totalInvestido: 'totalInvested', irpf: 'irpf', iof: 'iof', totalLiquido: 'netTotal', codigoProfissao: 'professionCOde', descricaoProfissao: 'professionDescription', tipoTransacao: 'type', movimentacaoExtrato: 'title', valor: 'value', dataHora: 'dateTime', saldoFinal: 'currentBalance', saldoDisponivel: 'balance', limiteChequeEspecial: 'overdraftLimit', saldoDisponivel: 'available', limiteTotalDisponivel: 'totalOverdraftLimit', totalInvestido: 'totalInvested', irpf: 'irpf', iof: 'iof', totalLiquido: 'netTotal', codigoProfissao: 'professionCode', descricaoProfissao: 'professionDescription' }); translate = prop => dictionary()[prop]; replaceKey = (str, key) => { if (translate(key)) { const regex = RegExp(`\\"(${key})\\":`, 'g') return str.replace(regex, `"${translate(key)}":`) } } printProp = json => { let jsonStr = JSON.stringify(json) const keys = Object.keys(dictionary()); keys.forEach(key => { jsonStr = replaceKey(jsonStr, key) }); return JSON.parse(jsonStr) }; const jsonTest = { saldoDisponivel: 'Test', limiteChequeEspecial: 24, totalLiquido: 7, saldoDisponivel: { irpf: 1, descricaoProfissao: 2 }, tipoTransacao: 'x' }; const jsonFinal = printProp(jsonTest); console.log(jsonFinal);
forEach no JSON
translate = prop => { const names = { tipoTransacao: 'type', movimentacaoExtrato: 'title', valor: 'value', dataHora: 'dateTime', saldoFinal: 'currentBalance', saldoDisponivel: 'balance', limiteChequeEspecial: 'overdraftLimit', saldoDisponivel: 'available', limiteTotalDisponivel: 'totalOverdraftLimit', totalInvestido: 'totalInvested', irpf: 'irpf', iof: 'iof', totalLiquido: 'netTotal', codigoProfissao: 'professionCOde', descricaoProfissao: 'professionDescription', tipoTransacao: 'type', movimentacaoExtrato: 'title', valor: 'value', dataHora: 'dateTime', saldoFinal: 'currentBalance', saldoDisponivel: 'balance', limiteChequeEspecial: 'overdraftLimit', saldoDisponivel: 'available', limiteTotalDisponivel: 'totalOverdraftLimit', totalInvestido: 'totalInvested', irpf: 'irpf', iof: 'iof', totalLiquido: 'netTotal', codigoProfissao: 'professionCOde', descricaoProfissao: 'professionDescription', tipoTransacao: 'type', movimentacaoExtrato: 'title', valor: 'value', dataHora: 'dateTime', saldoFinal: 'currentBalance', saldoDisponivel: 'balance', limiteChequeEspecial: 'overdraftLimit', saldoDisponivel: 'available', limiteTotalDisponivel: 'totalOverdraftLimit', totalInvestido: 'totalInvested', irpf: 'irpf', iof: 'iof', totalLiquido: 'netTotal', codigoProfissao: 'professionCode', descricaoProfissao: 'professionDescription' }; return names[prop]; }; replaceKey = (str, key) => { if (translate(key)) { const regex = RegExp(`\\"(${key})\\":`, 'g') return str.replace(regex, `"${translate(key)}":`) } } const toString = (json) => JSON.stringify(json) const toJSON = (str) => JSON.parse(str) printProp = json => { let jsonStr = toString(json) const keys = Object.keys(json); keys.forEach(item => { if (typeof json[item] === 'object') { const translatedJSON = printProp(json[item]); const parsedJSON = toJSON(jsonStr) parsedJSON[item] = translatedJSON jsonStr = toString(parsedJSON) } jsonStr = replaceKey(jsonStr, item) }); return toJSON(jsonStr) }; const jsonTest = { saldoDisponivel: 'Test', limiteChequeEspecial: 24, totalLiquido: 7, saldoDisponivel: { irpf: 1, descricaoProfissao: 2 }, tipoTransacao: 'x' }; const jsonFinal = printProp(jsonTest); console.log(jsonFinal);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach no dicionário
forEach no JSON
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
A code review! After analyzing the provided code, I'll focus on the `printProp` function and its usage. **The Problem** In the `printProp` function, there's an attempt to recursively process nested objects using `forEach`. However, this approach can lead to a problem when encountering arrays or other non-object values. The current implementation doesn't handle these cases correctly, which may cause unexpected behavior or errors. **The Issue** In the `jsonTest` object, you have an array value (`tipoTransacao`) that's not being handled as an array in the `printProp` function. When the function tries to iterate over the `keys` of the nested objects, it will attempt to call `forEach` on the array value, which is not allowed. **The Solution** To fix this issue, you can add a simple check to determine whether the value is an object or an array before attempting to iterate over its keys. Here's an updated version of the `printProp` function: ```javascript const printProp = json => { let jsonStr = toString(json) const keys = Object.keys(json); // Check if key is an array keys.forEach(key => { if (Array.isArray(json[key])) { const translatedJSON = JSON.stringify(json[key]); const parsedJSON = toJSON(jsonStr) parsedJSON[key] = translatedJSON jsonStr = toString(parsedJSON) } else if (typeof json[key] === 'object') { const translatedJSON = printProp(json[key]) const parsedJSON = toJSON(jsonStr) parsedJSON[key] = translatedJSON jsonStr = toString(parsedJSON) } jsonStr = replaceKey(jsonStr, key) }); return toJSON(jsonStr) }; ``` By adding the `Array.isArray` check, we ensure that arrays are handled correctly and don't cause unexpected behavior. **Additional Suggestions** * Consider using a more robust JSON processing library or a specific solution for handling nested objects and arrays. * You may want to explore alternative data structures, like an object with typed properties (e.g., `{ [key: string]: any }`), to avoid issues with dynamic property names. * If you're working with complex data structures, consider using a more advanced approach, such as using `JSON.parse()` and `JSON.stringify()` in combination with `Object.keys()` and `forEach()` to process the data correctly.
Related benchmarks:
comp test
str cmp 0
Class vs ID
Test JS endswith implementation
Test JS endswith implementation2
Comments
Confirm delete:
Do you really want to delete benchmark?