<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jan Wiemers on Medium]]></title>
        <description><![CDATA[Stories by Jan Wiemers on Medium]]></description>
        <link>https://medium.com/@janwiemers?source=rss-214411e5d619------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*m8dy4XT9Zi-_rMcOKDly4Q.jpeg</url>
            <title>Stories by Jan Wiemers on Medium</title>
            <link>https://medium.com/@janwiemers?source=rss-214411e5d619------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 23:55:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@janwiemers/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Do you really need jQuery?]]></title>
            <link>https://medium.com/@janwiemers/do-you-really-need-jquery-997f03b0da55?source=rss-214411e5d619------2</link>
            <guid isPermaLink="false">https://medium.com/p/997f03b0da55</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[jquery]]></category>
            <dc:creator><![CDATA[Jan Wiemers]]></dc:creator>
            <pubDate>Wed, 24 Jun 2015 19:14:33 GMT</pubDate>
            <atom:updated>2015-06-24T19:23:05.967Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*TnK3qIKRJgdOlp-8niW9rg.png" /></figure><p>This article take’s a project I was working on as an example but it could be transferred to nearly every situation in I think our all or at least my professional life on the frontend side of the web. But let’s start…</p><p>In most of the Projects I worked for we used jQuery like <a href="http://trends.builtwith.com/javascript/jQuery">many others</a>. As I started working for 1&amp;1 and worked for the search project of <a href="http://suche.web.de/">web.de</a> and <a href="http://suche.gmx.net/">gmx.net</a> in this project performance was an essential part of the software so I had a deeper look at the loaded assets and questioned myself what we can get rid of. In this case:</p><blockquote>“Do we really need jQuery?”</blockquote><p>I took a look at the parts of the API the project made use of and was honestly surprised by the result. The project just used five things:</p><ul><li>Element selection</li><li>Event handling</li><li>Ajax requests</li><li>add- /removeClass</li><li>fadeIn/Out animations</li></ul><blockquote>So lets rephrase we load 32kb minified and gzipped only for these five things.</blockquote><h3>Create it yourself</h3><p>Ok my Idea was to create a small set of utils to handle this. Two of the things are really easy to replace, “add- /removeClass” and “Ajax requests” are easy to write on your own.</p><p>But there are a few things left right? At this time the quality requirements forced me to support Internet Explorer 6 so I could not use cool native API’s like `querySelectorAll` But happily I had no need to support all the cool functions of the sizzle selector engine.</p><p>Element selection made simple. I just need to select `document`, `window` and a few elements where I can put an ID attribute on. This is quite simple to archive and reduces the complexity down to a few `if else` statements.</p><p>The next API to replace is the fadeIn/Out mechanism. I talked to the Designer and they said that this is just optical sugar and don’t have to work on every Browser and that it would be OK if it just work for modern browsers so we can go the progressiv enhancement way and I created a small functionality based on two CSS3 classes `.fade` and `.in` to fade elements in and out with `css transitions`.</p><p>The hardest thing to implement was the jQuery event handling. So I looked at the `on` usage in the project and tried to reduce the complexity of this method. It turned out that we don’t make use of a lot of cool things like event delegating and event removal. That where good news and it would be enough if we implement a method that only handles the cross-browser event name handling and accepts a callback function. Thats simple right?</p><p>The final implementation took care of all this and reduced the size to a small script of about 4kb, which had a big impact in performance and page load. Everything I had to to is to question myself, talk to the people and manager around the project convince and convince them to find a solution that fits to the performance goals and supports the further development of the project.</p><h3>Conclusion</h3><p>Don’t get me wrong… I honestly like jQuery but in terms of performance it is necessary to change your mind and to leave the development friendly ecosystem of third-party library’s and reshape the requirements to find a solution which fits the current needs and is prepared to be extensible in the future. So performance starts more in communication then in implementation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=997f03b0da55" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Override a native javascript interface]]></title>
            <link>https://medium.com/@janwiemers/override-a-native-javascript-interface-fbacb08d9614?source=rss-214411e5d619------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbacb08d9614</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[monitoring]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Jan Wiemers]]></dc:creator>
            <pubDate>Fri, 19 Jun 2015 08:11:32 GMT</pubDate>
            <atom:updated>2015-06-19T19:46:24.998Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*ZQF7rFsj-OUgR_PQIqOBiQ.png" /></figure><p>Ok, someone may ask now „why the hell should i want to override a javascript native interface?“ And the answer is a bit difficult because you are right. Overriding native code is always a risky idea because you have to think about each browser and my have to implement browser specific methods which is quit pain in the ass. But there are some cases and reasons where you can’t avoid to do it.</p><ol><li>The first one is you want to mock a interface to make a function testable without run into several dependencies. Like <a href="https://github.com/philikon/MockHttpRequest">MockHttpRequest</a> for example. This Library is written to make ajax requests testable and mockable, but to provide this functionality it has to mock the whole `XMLHttpRequest` Object. Simply look at the code, its quit a lot of stuff.</li><li>The second one is a usecase in which you want to gain control over specific parts of a native interface. This is what i did and want to describe below.</li></ol><h3>how to get the ajax timings?</h3><p>My journey in the native code started at exact this point. Behind this question was a task I had received from my company. We want to measure the “frontend performance” timings from our platform. Due to the fact that we already use jQuery on our front end we could easily use the `$.ajax` method to get all necessary data. That sound like an awesome easy way which we could provide in a very short time period. But (god I hate this word) there is a little Problem. We at XING use a “lazy load” mechanism to load most of our assets, javascript and css files, on demand. but this mechanism doesn’t use jQuery and it could load anywhere in time. So we had two options.</p><ol><li>We decide to not measure any asset and have no timings for our asset CDN</li><li>We build an javascript which provides us the possibility to track and measure ajax requests on a very low level.</li></ol><p>We choosed, as you might already thougt, the second option to get the full monitoring data.</p><h3>how to implement the hook</h3><p>First of all you have to make sure that your intervention is as small as possible. So I decided not to use the full `facade` pattern, because there are so many things and potential failure sources I have to think about. I decided to `bridge` the original Object, that means I override only the constructor to gain control over the instance and have the possibility to add my own hooks to that instance. Which has the lovely sideeffect that individual browser methods, if exists, are still there and I don’t have to care about because I return the original constructor at the end of my implementation.</p><p>And in my case it works quite well because I just want to get a timing from the beginning of a request until the request is complete and the url which is called. Translated to code I want to get the timing between `xhr.send()` and the time where `xhr.readyState` equals 4.</p><h3>let’s start to code</h3><p>Ok, let’s dive into the code and let’s see how to create a new XMLHttpRequest Instance.</p><pre><strong>var</strong> xhr <strong>=</strong> <strong>new</strong> XMLHttpRequest();</pre><p>So far so good and due to the fact that every ajax request has to go the same way, means to create a new instance from the `XMLHttpRequest` Object this is the point to add our logic to the code.</p><pre>(<strong>function</strong>(window){<br>  window.originalXMLHttpRequest <strong>=</strong> window.XMLHttpRequest;<br>  window.XMLHttpRequest <strong>=</strong> <strong>function</strong>(){}<br>})(window);</pre><p>With this small lines we get the force over the `XMLHttpRequest` Object because we override the original interface with our own one and store the native code in another variable so that we can access it and restore it if needed.</p><p>So let’s step ahead. We need one essential thing if we want that all scripts which uses the `XMLHttpRequest` interface still work after our “patch”. We need a fully functioning request.</p><pre>(<strong>function</strong>(window){<br>  window.originalXMLHttpRequest <strong>=</strong> window.XMLHttpRequest;<br>  window.XMLHttpRequest <strong>=</strong> <strong>function</strong>(){<br>    request <strong>=</strong> window.originalXMLHttpRequest();<br>    <strong>return</strong> request;<br>  }<br>})(window);</pre><p>Now we have a “bridge” which returns an exact copy of the native XMLHttpRequest object and we have one thing more which is quit useful. We have access to a variable which holds the instance of the ajax object and which we can use to add more hooks.</p><p>So how to proceed? We need the timings right? So we can add an eventListener to get the two timestamps.</p><pre>(<strong>function</strong>(window){<br>  window.originalXMLHttpRequest <strong>=</strong> window.XMLHttpRequest;<br>  window.XMLHttpRequest <strong>=</strong> <strong>function</strong>(){<br>    <em>/* create a instance from the original Object */</em><br>    <strong>var</strong> request <strong>=</strong> window.originalXMLHttpRequest();<br><br>    <em>/* the variable for the start time */</em><br>    <strong>var</strong> starttime;<br><br>    <em>/* add a hook to the readystate event */</em><br>    request.addEventlistener(&#39;readystatechange&#39;, <strong>function</strong>(){<br>      <strong>if</strong>(request.readyState <strong>==</strong> 1) {<br>          starttime <strong>=</strong> <strong>+new</strong> Date()<br>        }<br>        <strong>if</strong>(request.readyState <strong>==</strong> 4 {<br>          console.log(<strong>+new</strong> Date() <strong>-</strong> starttime;<br>        }<br>    }, <strong>false</strong>);<br><br>    <em>/* return the original interface */</em><br>    <strong>return</strong> request;<br>  }<br>})(window);</pre><p>Starting from know we get a `console.log` for every ajax request no matter if the request was called via vanillaJs, jQuery or any other framework. The only thing you have to make sure is that this script is loaded and executes first because most frameworks encapsulate the interface too.</p><h3>How to read the requested URL</h3><p>I my first thought about it was “this is easy going, the request will probably know its url otherwise he can’t do a call to it. And thats right on one hand, but wrong on the other. In fact its not a accessible property. That means the only person how knows the url is the developer who writes the code. So we need another hook to grab the url.</p><pre><strong>var</strong> originalOpen <strong>=</strong> request.open;<br><strong>this</strong>.open <strong>=</strong> <strong>function</strong>(method, url, async, user, password) {<br>  console.log(url);<br>  originalOpen.call(<strong>this</strong>, method, url, async, user, password);<br>}</pre><h3>Let’s put it all together</h3><pre>(<strong>function</strong>(window){<br>  window.originalXMLHttpRequest <strong>=</strong> window.XMLHttpRequest;<br>  window.XMLHttpRequest <strong>=</strong> <strong>function</strong>(){<br>    <em>/* create a instance from the original Object */</em><br>    <strong>var</strong> request <strong>=</strong> window.originalXMLHttpRequest();<br><br>    <em>/* the variable for the start time */</em><br>    <strong>var</strong> starttime;<br><br>    <em>/* store the origin open method */</em><br>    <strong>var</strong> originalOpen <strong>=</strong> request.open;<br><br>    <strong>this</strong>.open <strong>=</strong> <strong>function</strong>(method, url, async, user, password) {<br>      console.log(url);<br>      originalOpen.call(<strong>this</strong>, method, url, async, user, password);<br>    }<br><br>    <em>/* add a hook to the readystate event */</em><br>    request.addEventlistener(&#39;readystatechange&#39;, <strong>function</strong>(){<br>      <strong>if</strong>(request.readyState <strong>==</strong> 1) {<br>          starttime <strong>=</strong> <strong>+new</strong> Date()<br>        }<br>        <strong>if</strong>(request.readyState <strong>==</strong> 4 {<br>          console.log(<strong>+new</strong> Date() <strong>-</strong> starttime;<br>        }<br>    }, <strong>false</strong>);<br><br>    <em>/* return the original interface */</em><br>    <strong>return</strong> request;<br>  }<br>})(window);</pre><h3>Conclusion</h3><p>And that’s all. We get all the data we need and have just made a little surgical intervention, which minimizes the risk of failure and provides nearly the same stable and fast interface as the original `XMLHttpRequest` Object.</p><p>If you have any feedback, improvement or questions please get in touch with me.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbacb08d9614" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>