Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33243

Re: Object.defineProperties

From Cezary Tomczyk <cezary.tomczyk@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Object.defineProperties
Date 2017-05-06 23:19 +0200
Organization csiph.com Internet News Service
Message-ID <oelelh$jon$1@csiph.com> (permalink)
References (5 earlier) <1592229.TLkxdtWsSY@PointedEars.de> <oekitc$2tgv$1@csiph.com> <2032926.ElGaqSPkdT@PointedEars.de> <oel4aa$9ml$1@csiph.com> <1982276.iZASKD2KPV@PointedEars.de>

Show all headers | View raw


On 06/05/2017 22:21, Thomas 'PointedEars' Lahn wrote:
> Cezary Tomczyk wrote:
>
>> On 06/05/2017 16:43, Thomas 'PointedEars' Lahn wrote:
>>> Cezary Tomczyk wrote:
>>>> On 05/05/2017 20:11, Thomas 'PointedEars' Lahn wrote:
>>>>> To begin with, you should have
>>>>>
>>>>>   var _toString = {}.toString;
>>>>>
>>>>>   function isTypeOf(obj, typeToCheck) {
>>>>>     if (arguments.length < 2) {
>>>>>       return false;
>>>>>     }
>>>>>     return _toString.call(obj).slice(8, -1).toLowerCase() ===
>>>>> typeToCheck.toLowerCase();
>>>>>   }
>>>>
>>>> Tiny optimisation, but that's fine. :-)
>>>
>>> Tiny optimization with big impact, I think.  It saves
>>>
>>>   - 1 lookup along the scope chain up to the global object for “Object”
>>>     *every time the function is called*
>>>
>>>   - 2 lookups along the prototype chain each for “prototype” and
>>>   “toString”
>>>     *every time the function is called*
>>>
>>> and removes the dependency on a potentially non-initial value of
>>> “Object.prototype” and “Object.prototype.toString”.
>>
>> That's true, but it is also important to mention that the impact is
>> significant in case of hundred operations at the same time.
>
> It is more significant if this function is called several times in a single-
> threaded environment: the slower it is, the slower does the program run.

True and if optimization can be done without complexity I am fine with that.

>>>> Finally I rewrote it a bit:
>>>> […]
>>>>      function isTypeOf(obj, typeToCheck) {
>>>> […]
>>>>          if (arguments.length !== 2) {
>>> This change does not make sense to me.  “< 2” was clearer.
>>
>> Passing 1 argument doesn't make sense and that's why I've used !== 2.
>
> Yes, but …
>
>>>>              return false;
>>>>          }
>>>>
>>>>          objectType = typeof obj;
>>> IMO, DRY demands declaration and initialization in one statement, if
>>> possible.
>>
>> I did it on purpose. If argument will be !== 2 then it is not efficient
>> to initialize anything before.
>
>     [Your reply here does not have anything to do with what you are replying
>      to.]
>
> … why should the function return “false” if I pass 3 or more arguments to
> it?
>
> Besides, an insufficient number of arguments should throw an exception
> instead.

Agreed. In my other post I do throw new Error.

>>> The question remains: What is being *gained* by this comparably
>>> inefficient precision instead of duck typing?  _Not_ all things
>>> that *can* be done also *should* be done.
>>
>> The gain is to get the real type (let me call it like that) of object.
>
> Why is that important?

One of the reason is because I use it in other function that extends 
deeply one object with another. I can't use Object.assign as the changes 
on extended object must not mutate original object.

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/
https://www.aslint.org/ - Accessibility validation tool

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 08:36 +0200
  Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 10:14 +0200
    Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 10:59 +0200
      Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 12:36 +0200
        Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 18:47 +0200
          Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 18:18 +0200
    Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 18:14 +0200
      Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-05 19:37 +0200
        Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 20:11 +0200
          Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 15:26 +0200
            Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-06 16:43 +0200
              Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 20:23 +0200
                Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-06 22:21 +0200
                Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 23:19 +0200
                Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-07 02:40 +0200
            Re: Object.defineProperties Stefan Weiss <krewecherl@gmail.com> - 2017-05-06 22:37 +0200
              Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 23:05 +0200
                Re: Object.defineProperties Stefan Weiss <krewecherl@gmail.com> - 2017-05-07 00:29 +0200
                Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-07 10:06 +0200
                Re: Object.defineProperties Stefan Weiss <krewecherl@gmail.com> - 2017-05-08 00:50 +0200
                Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-08 13:17 +0200
                Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-07 02:37 +0200
                Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-07 09:42 +0200
                Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-07 17:11 +0200
                Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-07 20:08 +0200
        Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 20:59 +0200
          Re: Object.defineProperties John G Harris <niam@jghnorth.org.uk.invalid> - 2017-05-06 10:35 +0100
  Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-05 08:32 +0200
  Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 18:24 +0200

csiph-web