PowerShell v3 – Creating Objects With [pscustomobject] – it’s fast!

*****Warning. This is from a preview release******

PowerShell v2 brought the ability to create a custom object via the following method:

1 $CustomObject1 New-Object psobject -Property @{a=1; b=2; c=3; d=4}
2  
3 $CustomObject1 Format-List

PowerShell v3 brings the possibility to create a custom object via

[pscustomobject]

1 $CustomObject2 = [pscustomobject]@{a=1; b=2; c=3; d=4}
2  
3 $CustomObject2 Format-List

Note: both methods create a PSCustomObject with NoteProperties, not a hashtable object

1 $CustomObject1 Get-Member
2  
3 $CustomObject2 Get-Member

So, why would you want to do it this way? Well firstly it preserves the insertion order,which helps with my OCD issues again. However, the main reason I have seen so far is that it is also a lot quicker. Fellow PowerShell MVP Tome Tanasovski carried out some basic performance testing which I thought I would highlight here.

There are four different ways you can create a custom object and a typical use case would be using PowerShell for reporting purposes, e.g. iterating through a list of VMs and pulling out various properties of them to create a report. With a very basic example, let’s have a look at the speed differences:

1) Select-Object

Not everybody knows that it’s possible to create a custom object with Select-Object. This was a handy trick since v1 days and was pretty quick too.

1 $TestSelect = {
2 (0..5000) | ForEach-Object {$CustomObject "" Select-Object Name,ID
3 $CustomObject.Name = "Test Name"
4 $CustomObject.ID = $_
5 $CustomObject
6 }
7 }
8 Measure-Command $TestSelect Format-Table TotalSeconds -Autosize

2) Add-Member

1 $TestAddMember = {
2 (0..5000) | ForEach-Object {$CustomObject New-Objectpsobject
3 $CustomObject Add-Member -Name "Name" -Value "Test Name"
4 $CustomObject Add-Member -Name "ID" -Value $_
5 $CustomObject
6 }
7 }
8 Measure-Command $TestAddMember Format-Table TotalSeconds -Autosize

3) Property Parameter

1 $TestProperty = {
2 (0..5000) | ForEach-Object {New-Object psobject -Property@{Name = "Test Name"; ID = $_}}
3 }
4 Measure-Command $TestProperty Format-Table TotalSeconds -Autosize

4) [pscustomobject]

1 $TestProperty = {
2 (0..5000) | ForEach-Object {[pscustomobject]@{Name = "Test Name"; ID = $_}}
3 }
4 Measure-Command $TestPSCustomObject Format-TableTotalSeconds -Autosize

So a summary of the these basic testing results looks pretty good for [pscustomobject]!

Select-Object = 7.74s

Add-Member = 28.87s

Property = 7.29

[pscustomobject] = 0.94s

I hope to try out [pscustomobject] on some of my reporting scripts and see what difference it makes to real world testing.

PowerShell 中使用json对象的性能比较的更多相关文章

  1. MVC中处理Json和JS中处理Json对象

    MVC中处理Json和JS中处理Json对象 ASP.NET MVC 很好的封装了Json,本文介绍MVC中处理Json和JS中处理Json对象,并提供详细的示例代码供参考. MVC中已经很好的封装了 ...

  2. js中的json对象详细介绍

    JSON一种简单的数据格式,比xml更轻巧,在JavaScript中处理JSON数据不需要任何特殊的API或工具包,下面为大家详细介绍下js中的json对象, 1.JSON(JavaScript Ob ...

  3. 简单使用JSON,JavaScript中创建 JSON 对象(一)

    JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. ...

  4. js中 给json对象添加属性和json数组添加元素

    js中 给json对象添加新的属性 比如现在有一个json对象为jsonObj,需要给这个对象添加新的属性newParam,同时给newParam赋值为pre.做法如下: var obj={ &quo ...

  5. 利用reduce方法,对数组中的json对象去重

    数组中的json对象去重 var arr = [{ "name": "ZYTX", "age": "Y13xG_4wQnOWK1Q ...

  6. js中解析json对象:JSON.parse()用于从一个字符串中解析出json对象, JSON.stringify()用于从一个对象解析出字符串。

    JSON.parse()用于从一个字符串中解析出json对象. var str = '{"name":"huangxiaojian","age&quo ...

  7. java中的JSON对象的使用

    申明:没工作之前都没听过JSON,可能是自己太菜了.可能在前台AJAX接触到JSON,这几天要求在纯java的编程中,返回JSON字符串形式. 网上有两种解析JSON对象的jar包:JSON-lib. ...

  8. js中的json对象

    1.JSON(JavaScript Object  Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不须要 ...

  9. ios中解析json对象基类

    这个是对上面一篇写的一个解析json对象的基类 @interface BaseObjectFromJson : NSObject + (id) objectWithDict:(NSDictionary ...

随机推荐

  1. 深入理解linux网络技术内幕读书笔记(七)--组件初始化的内核基础架构

    Table of Contents 1 引导期间的内核选项 2 注册关键字 3 模块初始化代码 引导期间的内核选项 linux运行用户把内核配置选项传给引导记录,然后引导记录再把选项传给内核. 在引导 ...

  2. python遗传算法实现数据拟合

    python据说功能强大,触角伸到各个领域,网上搜了一下其科学计算和工程计算能力也相当强,具备各种第三方包,除了性能软肋外,其他无可指摘,甚至可以同matlab等专业工具一较高下. 从网上找了一个使用 ...

  3. mongoose--------ORM数据操作框架

    数据关系映射:ORM O:object R:relation M:mapping 把对数据库的操作都封装到对象中,操作了对象,就相当于操作了数据库. 安装: npm install mongoose ...

  4. Color 颜色码-英文名称-十六进制-RGB对照表

      色 英文代码 形像颜色 HEX格式 RGB格式 LightPink 浅粉红 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 Crimson 猩红 # ...

  5. Oracle Directory文件夹的知识

    在上一章介绍expdp/impdp时曾使用过DIRECTORY这个概念,以下再简单说明下DIRECTORY的点点滴滴. MOS上对DIRECTORY的解释(266875.1): (1).基于服务端 v ...

  6. [Angular 2] Refactoring mutations to enforce immutable data in Angular 2

    When a Todo property updates, you still must create a new Array of Todos and assign a new reference. ...

  7. 设计模式 - 命令模式(command pattern) 具体解释

    命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...

  8. 网页JavaScript1

    DOM的操作 windows对象操作 属性: opener,打开当前窗口的源窗口,首次启动 是null. dialogArgument,对话框的返回值 子对象: history , location ...

  9. WCF 客户端与服务端消息传输

    WCF很多需要认证信息,保证服务的安全,可以使用消息来实现 WCF 实现消息的方式: WCF中有两个接口: IClientMessageInspector [定义一个消息检查器对象,该对象可以添加到 ...

  10. 关于解决方案和web文件夹放在同一目录路径错误的问题

    今天公司要做个b2b商城,下了个源码,目的是在这个基础上改,可是源码没有解决方案,于是建立了个解决方案,然后添加网站,发现解决方案和web目录位于不同目录(解决方案总是自动生成一个目录),可是我从网上 ...