Skip to content

Instantly share code, notes, and snippets.

@jastisriradheshyam
Created October 8, 2019 15:16
Show Gist options
  • Select an option

  • Save jastisriradheshyam/3e69aeda1b169345e3834b7b8f891560 to your computer and use it in GitHub Desktop.

Select an option

Save jastisriradheshyam/3e69aeda1b169345e3834b7b8f891560 to your computer and use it in GitHub Desktop.
removal of RowDataPacket from the results of mysql query in node js
'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);
}
});
});
};
'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);
}
});
});
};
@j8jacobs
Copy link

j8jacobs commented Oct 9, 2019

This is awesome! Good initiative.

@georgebullock
Copy link

Thanks for this. Very cool. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment