Created
October 8, 2019 15:16
-
-
Save jastisriradheshyam/3e69aeda1b169345e3834b7b8f891560 to your computer and use it in GitHub Desktop.
removal of RowDataPacket from the results of mysql query in node js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var pool = require('./connection'); | |
| const mysql = require('mysql'); | |
| /** | |
| * Executes SQL query and returns data. | |
| * @constructor | |
| * @param {string} queryText - SQL query string. | |
| */ | |
| const querySQL = function (queryText) { | |
| return new Promise(function (resolve, reject) { | |
| pool.query(queryText, function (err, results, fields) { | |
| // Error | |
| if (err) return reject(err); | |
| if (Array.isArray(results)) { | |
| // removal by for loop | |
| let finalResults = []; | |
| const resultsLength = results.length; | |
| for (let index = 0; index < resultsLength; index++) { | |
| finalResults.push({...results[index]}); | |
| } | |
| return resolve(finalResults); | |
| } else { | |
| return resolve(results); | |
| } | |
| }); | |
| }); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var pool = require('./connection'); | |
| const mysql = require('mysql'); | |
| /** | |
| * Executes SQL query and returns data. | |
| * @constructor | |
| * @param {string} queryText - SQL query string. | |
| */ | |
| const querySQL = function (queryText) { | |
| return new Promise(function (resolve, reject) { | |
| pool.query(queryText, function (err, results, fields) { | |
| // Error | |
| if (err) return reject(err); | |
| if (Array.isArray(results)) { | |
| // removal by JSON stringifing and parsing stingified data | |
| const startJSON = process.hrtime(); | |
| const JSONResults = JSON.parse(JSON.stringify(results)); | |
| const endJSON = process.hrtime(startJSON); | |
| // console.log(JSONResults); | |
| console.log("\n%d nanoseconds json", endJSON[1]); | |
| // removal by Map function | |
| const startMAP = process.hrtime(); | |
| const objectifyRawPacket = row => ({ ...row }); | |
| // iterate over all items and convert the raw packet row -> js object | |
| const MapResults = results.map(objectifyRawPacket); | |
| const endMAP = process.hrtime(startMAP); | |
| // console.log(MapResults) | |
| console.log("\n%d nanoseconds map", endMAP[1]); | |
| // removal by for loop, similar to above Map function method | |
| const startFor = process.hrtime(); | |
| let forResults = []; | |
| for (let index = 0; index < results.length; index++) { | |
| forResults.push({...results[index]}); | |
| } | |
| const endFor = process.hrtime(startFor); | |
| // console.log(forResults); | |
| console.log("\n%d nanoseconds for", endFor[1]); | |
| // removal by object assign method | |
| const startAssign = process.hrtime(); | |
| const assignResults = []; | |
| for (let index = 0; index < results.length; index++) { | |
| const element = Object.assign({}, results[index]); | |
| assignResults.push(element); | |
| } | |
| const endAssign = process.hrtime(startAssign); | |
| // console.log(assignResults) | |
| console.log("\n%d nanoseconds assign", endAssign[1]); | |
| console.log("\n") | |
| return resolve(forResults); | |
| } else { | |
| return resolve(results); | |
| } | |
| }); | |
| }); | |
| }; |
Author
This is awesome! Good initiative.
Thanks for this. Very cool. 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Object.assign()does shallow cloning but it is faster of all) and faster processingquerySQLten times for same query for averaging the time results: