Consider I am saving a cookie in my Node.js app where I am using express framework's cookieParser middleware.

app.use(express.cookieParser()); app.get('/setCookie', function(req, res){ res.cookie('String_cookieName', 'String_value', { expires: new Date(Date.now() + 18000000), httpOnly: true }); // +5 Hours expiry });

And while reading them back, I came across two choices:

request.cookies['String_cookieName'] request.cookies.name

Both will return me the string_value , which I have set for my cookie ( 'String_cookieName' ) else if expired undefined will be returned.

But my question is which one is faster/efficient in terms of performance?

Problem courtesy of: Amol M Kulkarni

Solution

The performance is effectively the same, see jsperf.

A lot of the cookies examples use square brackets with the property as a string because often have characters which are not directly accessible using using dot notation.

Example:

cookies['connect.sid'] // OK cookies.connect.sid // error

Solution courtesy of: ruffrey

Discussion

There is no difference. It's just javascript has two ways of accessing fields of objects.

On my opinion it is better to use plain notation like request.cookies.name then request.cookies['name'] unless you are looping through many fields of an object and apply the same action for each of them. //lets say you have object like one below: var someObj = {}; someObj.x1 = 1; someObj.x2 = 2; someObj.x3 = 3; someObj.x4 = 4; someObj.x5 = 5; ... someObj.x20 = 20; //And if you want to output properties 3 to 12 //you can do like that for(var i = 3; i<12; ++i) { console.log(someObj['x' + i]); }

Otherwie just use plain notation, that way you'll keep code more readabale, and it will be easier to understand by fellow developers

Discussion courtesy of: Michael Tkachuk

Request.cookies is just an object. You can access any member of an object by using either of the methods that you wrote. object.xxx is typically used when you already know the name of the field you are accessing, where as brackets are generally used when the field name is dynamic; for example:

var variable = "foo"; object[variable] = "bar"; variable = "test"; object[variable] = "qwax" console.log(object.foo); //"bar" console.log(object.test); //"qwax"

As far as I know, neither is significantly faster than the other, so if you are accessing a static field, you can use them interchangeably.

Discussion courtesy of: Ari

This recipe can be found in it's original form on Stack Over Flow .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:What is the difference between request.cookies['name'] and request.cookies.name ...
本站链接:https://www.codesec.net/view/611546.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(58)