node省市區(qū)三級數(shù)據(jù)性能測評實例分析
更新時間:2019年11月06日 10:01:24 作者:蒼青浪
這篇文章主要介紹了node省市區(qū)三級數(shù)據(jù)性能,結合具體實例形式評測分析了node省市區(qū)三級數(shù)據(jù)的實現(xiàn)、改進方法與運行效率,需要的朋友可以參考下
本文實例講述了node省市區(qū)三級數(shù)據(jù)性能測評。分享給大家供大家參考,具體如下:
閑來無事,測試下node和egg
首先是數(shù)據(jù)庫,大概長這樣
然后是代碼
'use strict'; const Controller = require('egg').Controller; class HomeController extends Controller { async index() { const { ctx } = this; ctx.body = 'hi, egg'; } async city() { const { ctx } = this; console.time("sql") const provinces = await this.app.mysql.select('provinces') const citys = await this.app.mysql.select('cities') const areas = await this.app.mysql.select('areas') console.timeEnd("sql") console.time('cal') provinces.forEach(province => { let provinceid = province.provinceid province.children = [] citys.forEach(city => { city.children = [] if (city.provinceid === provinceid) { province.children.push(city) } let cityid = city.cityid areas.forEach(area => { if (area.cityid === cityid) { city.children.push(area) } }) }) }) console.timeEnd('cal') const result = { status: 1, data: provinces, } ctx.body = result; } } module.exports = HomeController;
執(zhí)行時間:
接著改進
'use strict'; const Controller = require('egg').Controller; class HomeController extends Controller { async index() { const { ctx } = this; ctx.body = 'hi, egg'; } async city() { const { ctx } = this; console.time("sql") let provinces = await this.app.mysql.select('provinces') let citys = await this.app.mysql.select('cities') let areas = await this.app.mysql.select('areas') console.timeEnd("sql") console.time('cal') for (let i = 0, len = citys.length; i < len; i++) { let city = citys[i] city.children = [] let cityid = city.cityid for (let j = 0, len1 = areas.length; j < len1; j++) { let area = areas[j] if (area.cityid === cityid) { city.children.push(areas.splice(j, 1)[0]) len1-- j-- } } } provinces.forEach(province => { let provinceid = province.provinceid province.children = [] for (let i = 0, len = citys.length; i < len; i++) { let city = citys[i] if (city.provinceid === provinceid) { province.children.push(city) citys.splice(i, 1) len-- i-- } } }) console.timeEnd('cal') const result = { status: 1, data: provinces, } ctx.body = result; } } module.exports = HomeController;
本次優(yōu)化結果
可以看到,在組裝數(shù)據(jù)的過程中,時間縮短了近20倍!
后續(xù)版本繼續(xù)優(yōu)化,也歡迎有相關方面經(jīng)驗的大神留言探討,給出更好的方案。
希望本文所述對大家node.js程序設計有所幫助。
您可能感興趣的文章:
- nodejs+mongodb aggregate級聯(lián)查詢操作示例
- Node.js+ES6+dropload.js實現(xiàn)移動端下拉加載實例
- node微信開發(fā)之獲取access_token+自定義菜單
- 詳解nodejs微信公眾號開發(fā)——6.自定義菜單
- JavaScript NodeTree導航欄(菜單項JSON類型/自制)
- nodejs 實現(xiàn)模擬form表單上傳文件
- Nodejs之http的表單提交
- Nodejs下用submit提交表單提示cannot post錯誤的解決方法
- 全面解析node 表單的圖片上傳
- Node層模擬實現(xiàn)multipart表單的文件上傳示例
- 推薦一個基于Node.js的表單驗證庫
相關文章
Node.js學習之TCP/IP數(shù)據(jù)通訊(實例講解)
下面小編就為大家?guī)硪黄狽ode.js學習之TCP/IP數(shù)據(jù)通訊(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10利用nodejs讀取圖片并將二進制數(shù)據(jù)轉換成base64格式
這篇文章主要介紹了利用nodejs讀取圖片并將二進制數(shù)據(jù)轉換成base64格式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Node.js中使用計時器定時執(zhí)行函數(shù)詳解
這篇文章主要介紹了Node.js中使用計時器定時執(zhí)行函數(shù)詳解,本文使用了Node.js中的setTimeout和setInterval函數(shù),需要的朋友可以參考下2014-08-08