Skip to content

Commit b986a9d

Browse files
committed
performance improvements
1 parent 5ce81d4 commit b986a9d

File tree

7 files changed

+110
-134
lines changed

7 files changed

+110
-134
lines changed

Gruntfile.js

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ module.exports = function(grunt) {
2828
src: [
2929
'src/header.js',
3030
'src/importer.js',
31-
'src/slowloop.js',
3231
'src/parser.js'
3332
],
3433
dest: 'mysql-import.js',

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<img src='https://i.imgur.com/AOfuTLA.png'>
55
</p>
66

7-
*Version 5.0.26* ([NPM](https://www.npmjs.com/package/mysql-import)) ([Github](https://github.com/Pamblam/mysql-import/))
7+
*Version 5.1.1* ([NPM](https://www.npmjs.com/package/mysql-import)) ([Github](https://github.com/Pamblam/mysql-import/))
88

99
[![Build Status](https://api.travis-ci.org/Pamblam/mysql-import.svg?branch=master)](https://travis-ci.org/Pamblam/mysql-import/) [![Coverage Status](https://coveralls.io/repos/github/Pamblam/mysql-import/badge.svg?branch=master)](https://coveralls.io/github/Pamblam/mysql-import?branch=master)
1010

mysql-import.js

+55-67
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* mysql-import - v5.0.26
2+
* mysql-import - v5.1.1
33
* Import .sql into a MySQL database with Node.
44
* @author Rob Parham
55
* @website https://github.com/pamblam/mysql-import#readme
@@ -16,7 +16,7 @@ const stream = require('stream');
1616

1717
/**
1818
* mysql-import - Importer class
19-
* @version 5.0.26
19+
* @version 5.1.1
2020
* https://github.com/Pamblam/mysql-import
2121
*/
2222

@@ -133,19 +133,26 @@ class Importer{
133133
this._current_file_no = 0;
134134

135135
var error = null;
136-
await slowLoop(files, (file, index, next)=>{
137-
this._current_file_no++;
138-
if(error){
139-
next();
140-
return;
141-
}
142-
this._importSingleFile(file).then(()=>{
143-
next();
144-
}).catch(err=>{
145-
error = err;
146-
next();
136+
137+
138+
139+
for(let i=0; i<files.length; i++){
140+
let file = files[i];
141+
await new Promise(next=>{
142+
this._current_file_no++;
143+
if(error){
144+
next();
145+
return;
146+
}
147+
this._importSingleFile(file).then(()=>{
148+
next();
149+
}).catch(err=>{
150+
error = err;
151+
next();
152+
});
147153
});
148-
});
154+
}
155+
149156
if(error) throw error;
150157
await this.disconnect();
151158
resolve();
@@ -325,37 +332,42 @@ class Importer{
325332
var full_paths = [];
326333
var error = null;
327334
paths = [].concat.apply([], paths); // flatten array of paths
328-
await slowLoop(paths, async (filepath, index, next)=>{
329-
if(error){
330-
next();
331-
return;
332-
}
333-
try{
334-
await this._fileExists(filepath);
335-
var stat = await this._statFile(filepath);
336-
if(stat.isFile()){
337-
if(filepath.toLowerCase().substring(filepath.length-4) === '.sql'){
338-
full_paths.push({
339-
file: path.resolve(filepath),
340-
size: stat.size
341-
});
342-
}
343-
next();
344-
}else if(stat.isDirectory()){
345-
var more_paths = await this._readDir(filepath);
346-
more_paths = more_paths.map(p=>path.join(filepath, p));
347-
var sql_files = await this._getSQLFilePaths(...more_paths);
348-
full_paths.push(...sql_files);
335+
336+
for(let i=0; i<paths.length; i++){
337+
let filepath = paths[i];
338+
await new Promise(async next=>{
339+
if(error){
349340
next();
350-
}else{
351-
/* istanbul ignore next */
341+
return;
342+
}
343+
try{
344+
await this._fileExists(filepath);
345+
var stat = await this._statFile(filepath);
346+
if(stat.isFile()){
347+
if(filepath.toLowerCase().substring(filepath.length-4) === '.sql'){
348+
full_paths.push({
349+
file: path.resolve(filepath),
350+
size: stat.size
351+
});
352+
}
353+
next();
354+
}else if(stat.isDirectory()){
355+
var more_paths = await this._readDir(filepath);
356+
more_paths = more_paths.map(p=>path.join(filepath, p));
357+
var sql_files = await this._getSQLFilePaths(...more_paths);
358+
full_paths.push(...sql_files);
359+
next();
360+
}else{
361+
/* istanbul ignore next */
362+
next();
363+
}
364+
}catch(err){
365+
error = err;
352366
next();
353367
}
354-
}catch(err){
355-
error = err;
356-
next();
357-
}
358-
});
368+
});
369+
}
370+
359371
if(error){
360372
reject(error);
361373
}else{
@@ -369,34 +381,10 @@ class Importer{
369381
/**
370382
* Build version number
371383
*/
372-
Importer.version = '5.0.26';
384+
Importer.version = '5.1.1';
373385

374386
module.exports = Importer;
375387

376-
/**
377-
* Execute the loopBody function once for each item in the items array,
378-
* waiting for the done function (which is passed into the loopBody function)
379-
* to be called before proceeding to the next item in the array.
380-
* @param {Array} items - The array of items to iterate through
381-
* @param {Function} loopBody - A function to execute on each item in the array.
382-
* This function is passed 3 arguments -
383-
* 1. The item in the current iteration,
384-
* 2. The index of the item in the array,
385-
* 3. A function to be called when the iteration may continue.
386-
* @returns {Promise} - A promise that is resolved when all the items in the
387-
* in the array have been iterated through.
388-
*/
389-
function slowLoop(items, loopBody) {
390-
return new Promise(f => {
391-
/* istanbul ignore next */
392-
if(!items.length) return f();
393-
let done = arguments[2] || f;
394-
let idx = arguments[3] || 0;
395-
let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
396-
loopBody(items[idx], idx, cb);
397-
});
398-
}
399-
400388

401389
class queryParser extends stream.Writable{
402390

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"memory-test": "node_modules/.bin/mocha ./test/memory-stress-test.js --timeout 0",
3939
"coverage": "nyc report --reporter=text-lcov | coveralls"
4040
},
41-
"version": "5.0.26",
41+
"version": "5.1.1",
4242
"dependencies": {
4343
"mysql2": "^2.3.3"
4444
}

src/importer.js

+52-40
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,26 @@ class Importer{
118118
this._current_file_no = 0;
119119

120120
var error = null;
121-
await slowLoop(files, (file, index, next)=>{
122-
this._current_file_no++;
123-
if(error){
124-
next();
125-
return;
126-
}
127-
this._importSingleFile(file).then(()=>{
128-
next();
129-
}).catch(err=>{
130-
error = err;
131-
next();
121+
122+
123+
124+
for(let i=0; i<files.length; i++){
125+
let file = files[i];
126+
await new Promise(next=>{
127+
this._current_file_no++;
128+
if(error){
129+
next();
130+
return;
131+
}
132+
this._importSingleFile(file).then(()=>{
133+
next();
134+
}).catch(err=>{
135+
error = err;
136+
next();
137+
});
132138
});
133-
});
139+
}
140+
134141
if(error) throw error;
135142
await this.disconnect();
136143
resolve();
@@ -310,37 +317,42 @@ class Importer{
310317
var full_paths = [];
311318
var error = null;
312319
paths = [].concat.apply([], paths); // flatten array of paths
313-
await slowLoop(paths, async (filepath, index, next)=>{
314-
if(error){
315-
next();
316-
return;
317-
}
318-
try{
319-
await this._fileExists(filepath);
320-
var stat = await this._statFile(filepath);
321-
if(stat.isFile()){
322-
if(filepath.toLowerCase().substring(filepath.length-4) === '.sql'){
323-
full_paths.push({
324-
file: path.resolve(filepath),
325-
size: stat.size
326-
});
327-
}
328-
next();
329-
}else if(stat.isDirectory()){
330-
var more_paths = await this._readDir(filepath);
331-
more_paths = more_paths.map(p=>path.join(filepath, p));
332-
var sql_files = await this._getSQLFilePaths(...more_paths);
333-
full_paths.push(...sql_files);
320+
321+
for(let i=0; i<paths.length; i++){
322+
let filepath = paths[i];
323+
await new Promise(async next=>{
324+
if(error){
334325
next();
335-
}else{
336-
/* istanbul ignore next */
326+
return;
327+
}
328+
try{
329+
await this._fileExists(filepath);
330+
var stat = await this._statFile(filepath);
331+
if(stat.isFile()){
332+
if(filepath.toLowerCase().substring(filepath.length-4) === '.sql'){
333+
full_paths.push({
334+
file: path.resolve(filepath),
335+
size: stat.size
336+
});
337+
}
338+
next();
339+
}else if(stat.isDirectory()){
340+
var more_paths = await this._readDir(filepath);
341+
more_paths = more_paths.map(p=>path.join(filepath, p));
342+
var sql_files = await this._getSQLFilePaths(...more_paths);
343+
full_paths.push(...sql_files);
344+
next();
345+
}else{
346+
/* istanbul ignore next */
347+
next();
348+
}
349+
}catch(err){
350+
error = err;
337351
next();
338352
}
339-
}catch(err){
340-
error = err;
341-
next();
342-
}
343-
});
353+
});
354+
}
355+
344356
if(error){
345357
reject(error);
346358
}else{

src/slowloop.js

-23
This file was deleted.

test/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RESET THEM TO '' BEFORE COMMITING CHANGES!
33
const mysql_host = '127.0.0.1';
44
const mysql_user = 'root';
5-
const mysql_pass = '';
5+
const mysql_pass = 'bijoux22';
66

77
const expect = require('chai').expect;
88
const {errorHandler,query,mysqlConnect,createTestDB,destroyTestDB,closeConnection} = require('./test-helpers.js');

0 commit comments

Comments
 (0)