search
HomeWeb Front-endJS TutorialImplement code through regular formatted url query string_javascript skills

I saw that the query string was formatted through the js array split method in the project, and I suddenly wondered why I couldn't use regular expressions. How is the performance? So we have the following code:

Copy code The code is as follows:

var url='www .baidu.com?a=123&b=456&c=789&e=dfsdfsdfsdfsdfsdfsdf&f=46545454545454785&g=e23232dsfvdfvdf';
/**
* Formatted query string (regular implementation)
* @param url url address
* @return {Object} Formatted json object
*/
function formatUrl(url){
var reg=/(?: [?&] )([^&] )=([^&] )/g;
var data={};
function fn(str,pro,value){
data[decodeURIComponent( pro)]=decodeURIComponent(value);
}
url.replace(reg,fn);
return data;
}
/**
* Formatted query string (array implementation)
* @param url url address
* @return {Object} Formatted json object
*/
function formatUrl2(url){
url=url.replace(/.*?/,'');
var args={},
items=url.length?url.split("&") :[]
,item=null
,i=0
,len=items.length;
for(i=0;iitem= items[i].split("=");
args[decodeURIComponent(item[0])]=decodeURIComponent(item[1]);
}
return args;
}
var startTime=new Date();
for(var i=0;iformatUrl2(url);
}
console.log('formatUrl2',( new Date()-startTime)); //formatUrl2 12138
startTime=new Date();
for(var i=0;iformatUrl(url);
}
console.log('formatUrl',(new Date()-startTime)); //formatUrl 12537

The test browser is chrme 25; the regular implemented function is actually better than the array The implemented function should be slower (tears...). But fortunately, it is only 0.4 seconds slower when repeated a million times.
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
提升Java时间日期格式化解析性能的方法提升Java时间日期格式化解析性能的方法Jul 01, 2023 am 08:07 AM

如何优化Java开发中的时间日期格式化解析性能摘要:在Java开发中,时间日期格式化与解析是常见的操作,但是由于时间日期格式复杂多样且处理的数据量庞大,往往会成为性能瓶颈。本文将介绍几种优化Java开发中时间日期格式化解析性能的方法,包括使用缓存、减少对象创建、选择适当的API等。一、引言时间日期格式化与解析在Java开发中非常常见。然而,在实际应用中,由于

修复:Rufus 无法在 Windows PC 中创建可启动 USB 问题修复:Rufus 无法在 Windows PC 中创建可启动 USB 问题Apr 29, 2023 am 09:19 AM

Rufus是一款出色的工具,可以轻松创建可启动的USB驱动器。这个小巧而时尚的工具的效率令人惊叹,通常可以提供无错误的操作。但是,有时创建一个新的可启动USB记忆棒会弹出一些错误消息,从而在刻录过程中绊倒。如果您在使用Rufus时遇到任何困难,您可以按照以下步骤为您的问题找到快速解决方案。修复1–运行驱动器的错误检查器在使用Rufus重试之前,您可以运行驱动器的错误检查器工具来扫描驱动器是否存在任何错误。1.同时按下Windows键+E键打开文件资源管理器。然后,点击“这台电脑

如何用 Golang 正则匹配多个单词或字符串?如何用 Golang 正则匹配多个单词或字符串?May 31, 2024 am 10:32 AM

Golang正则表达式使用管道符|来匹配多个单词或字符串,将各个选项作为逻辑OR表达式分隔开来。例如:匹配"fox"或"dog":fox|dog匹配"quick"、"brown"或"lazy":(quick|brown|lazy)匹配"Go"、"Python"或"Java":Go|Python|Java匹配单词或4位邮政编码:([a-zA

如何用php正则替换以什么开头的字符串如何用php正则替换以什么开头的字符串Mar 24, 2023 pm 02:57 PM

PHP正则表达式是一种针对文本处理和转换的有力工具。它可以通过解析文本内容,并按照特定的模式进行替换或截取,达到有效管理文本信息的目的。其中,正则表达式的一个常见应用是替换以特定字符开头的字符串,对此,我们进行如下的讲解

php 如何用正则去除中文php 如何用正则去除中文Mar 03, 2023 am 10:12 AM

php用正则去除中文的方法:1、创建一个php示例文件;2、定义一个含有中文和英文的字符串;3、通过“preg_replace('/([\x80-\xff]*)/i','',$a);”正则方法去除查询结果中的中文字符即可。

格式化u盘有什么后果格式化u盘有什么后果Jan 13, 2021 pm 05:32 PM

格式化u盘的后果:1、清空U盘,会将u盘中的文件全部删除;2、可以消除U盘上的一些逻辑性的错误和非顽固性病毒或流氓程序;3、如果经常性反复格式化u盘,会降低U盘的使用寿命。

格式化数据分区是什么意思?格式化数据分区是什么意思?Mar 10, 2023 am 11:41 AM

格式化数据分区就是对指定磁盘中的数据分区进行初始化操作,这种操作通常会导致现有分区中所有的文件被清除。磁盘格式化牵涉两个不同的程序:低级与高级格式化。前者处理盘片表面格式化赋与磁片扇区数的特质;低级格式化完成后,硬件盘片控制器(disk controller)即可看到并使用低级格式化的成果;后者处理“伴随着操作系统所写的特定信息”。

手机格式化后能恢复数据吗手机格式化后能恢复数据吗Jun 30, 2023 pm 02:12 PM

手机格式化之后还能恢复数据,只要对手机的数据进行备份就可以恢复。很多手机都自带数据备份功能,若想要恢复被删除或格式化的数据,可以点开手机自带备份功能,按照页面指示进行备份操作。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools