欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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程序設計有所幫助。

相關文章

  • Node.js學習之TCP/IP數(shù)據(jù)通訊(實例講解)

    Node.js學習之TCP/IP數(shù)據(jù)通訊(實例講解)

    下面小編就為大家?guī)硪黄狽ode.js學習之TCP/IP數(shù)據(jù)通訊(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • node高并發(fā)原理機制解讀

    node高并發(fā)原理機制解讀

    這篇文章主要介紹了node高并發(fā)原理機制,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • node.js微信公眾平臺開發(fā)教程

    node.js微信公眾平臺開發(fā)教程

    這篇文章主要為大家分享了node.js微信公眾平臺開發(fā)教程,如何進行微信開發(fā),感興趣的小伙伴們可以參考一下
    2016-03-03
  • node.js通過url讀取文件

    node.js通過url讀取文件

    這篇文章主要為大家詳細介紹了node.js通過url讀取文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Express框架實現(xiàn)簡單攔截器功能示例

    Express框架實現(xiàn)簡單攔截器功能示例

    這篇文章主要介紹了Express框架實現(xiàn)簡單攔截器功能,結合實例形式分析了express框架攔截器相關功能與使用方法,需要的朋友可以參考下
    2023-05-05
  • Node.js異步I/O學習筆記

    Node.js異步I/O學習筆記

    這篇文章主要介紹了Node.js異步I/O學習筆記,本文詳細講解了異步I/O的基本概念、Node的異步I/O、非I/O的異步API、事件驅動與高性能服務器等內(nèi)容,需要的朋友可以參考下
    2014-11-11
  • 利用nodejs讀取圖片并將二進制數(shù)據(jù)轉換成base64格式

    利用nodejs讀取圖片并將二進制數(shù)據(jù)轉換成base64格式

    這篇文章主要介紹了利用nodejs讀取圖片并將二進制數(shù)據(jù)轉換成base64格式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Node.js基礎入門之緩存區(qū)與文件操作詳解

    Node.js基礎入門之緩存區(qū)與文件操作詳解

    Node.js是一個基于Chrome?V8引擎的JavaScript運行時。類似于Java中的JRE,.Net中的CLR。本文將詳細為大家介紹Node.js中的緩存區(qū)與文件操作,感興趣的可以了解一下
    2022-03-03
  • M2實現(xiàn)Nodejs項目自動部署的方法步驟

    M2實現(xiàn)Nodejs項目自動部署的方法步驟

    這篇文章主要介紹了M2實現(xiàn)Nodejs項目自動部署的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • Node.js中使用計時器定時執(zhí)行函數(shù)詳解

    Node.js中使用計時器定時執(zhí)行函數(shù)詳解

    這篇文章主要介紹了Node.js中使用計時器定時執(zhí)行函數(shù)詳解,本文使用了Node.js中的setTimeout和setInterval函數(shù),需要的朋友可以參考下
    2014-08-08

最新評論